-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InMemory - bad key management for derived entity types #19854
Comments
@saliksaly I was able to reproduce this and it does look like a bug. Note for team: it looks like the code that peeks into the in-memory database to decide what key value to use is not working correctly when there is inheritance mapping. |
Hi, would not be there any workaround until 5.0.0 version? |
@saliksaly Just wanted to let you know that I have started investigating a workaround, but it may take me a while. |
Fixes #19854 Because in-memory isn't "TPH" and so derived types have their own "table" but the value generator is stored on the "table" that the property is defined in.
@saliksaly I have a PR out to fix this for EF Core 5.0. Here's a workaround for 3.1: private static void BumpGenerator(
DbContext context, Type entityClrType, string propertyName, int newLowBound)
{
var entityType = context.Model.FindEntityType(entityClrType);
var property = entityType.FindProperty(propertyName);
var options = context.GetService<IDbContextOptions>().FindExtension<InMemoryOptionsExtension>();
var inMemoryStore = context.GetService<IInMemoryStoreCache>().GetStore(options.StoreName);
var generator = inMemoryStore.GetIntegerValueGenerator<int>(property);
var values = new object[entityType.GetDeclaredProperties().Count()];
values[property.GetIndex()] = newLowBound;
generator.Bump(values);
} Use it like this: BumpGenerator(context, typeof(AnimalBase), nameof(AnimalBase.Id), 2);
context.Add(
new Cat
{
Name = "Tom",
}); Passing Note: this workaround uses EF internal code. This code will change in new releases of EF Core and the workaround will likely break. Use only with EF Core 3.1. |
Fixes #19854 Because in-memory isn't "TPH" and so derived types have their own "table" but the value generator is stored on the "table" that the property is defined in.
Fixes #19854 Because in-memory isn't "TPH" and so derived types have their own "table" but the value generator is stored on the "table" that the property is defined in.
Fixes #19854 Because in-memory isn't "TPH" and so derived types have their own "table" but the value generator is stored on the "table" that the property is defined in.
@ajcvickers
Please let me know if there is any workaround? |
@ShoSashko There is a workaround posted above. |
@ajcvickers Thanks for the quick response. Not sure how I can apply this workaround for my specific case, having a
But it throwing exception of the I have doubts that it will work anyway, there is no equal method for Please let me know if I am even on the right track. |
@ShoSashko No, this code doesn't make sense for strings, but then usually automatically generating string key values also doesn't make sense. You may be running into this breaking change. |
I have applied the fix to the demo app: I works now, no problems. |
Using in-memory database, ef core 3.1. (or 3.0)
Having abstract class Animal and two derived classes: Cat and Dog.
All are registered as entities in DbContext.
Seeding 2 Cat entities in DbContext.OnModelCreating.
1st problem:
When inserting a new Cat entity to DbSet, receiving error: "ArgumentException: An item with the same key has already been added. Key: 1".
The Id for the new entity shuld be 3 because there are already two.
2nd problem:
When inserting a new Dog entity to DbSet, it behaves inappropriately: replaces Cat entity with id 1 with the new Dog entity and adds new Dog entity with id 1!
Steps to reproduce
Here is asp net core demo app to demonstarte the problem described:
https://github.com/saliksaly/aspnetcoreapp-efcore-inherited-entity-id-problem
Further technical details
EF Core version: 3.1.0
Database provider: Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET Core 3.1
Operating system: Win10 v. 1809
IDE: Visual Studio Community 2019 16.4.4
The text was updated successfully, but these errors were encountered: