You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
InMemory - bad key management for derived entity types #19854
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!
@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.
Passing 2 here means that the next value generated will be 3.
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.
@ajcvickers
I just wanted to clarify. I have the same issue but with string as a key.
EF Core version: 3.1.0
Database provider: Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET Core 3.1
{"An item with the same key has already been added. Key: [email protected]"}
Not sure how I can apply this workaround for my specific case, having a string as a PK.
I've tried to change the workaround posted above from int to string
private static void BumpGenerator(
DbContext context, Type entityClrType, string propertyName, string 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<string>(property);
var values = new object[entityType.GetDeclaredProperties().Count()];
values[property.GetIndex()] = newLowBound;
generator.Bump(values);
}
But it throwing exception of the generator.Bump.
I have doubts that it will work anyway, there is no equal method for GetIntegerValueGenerator for the string.
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.
Activity
ajcvickers commentedon Feb 12, 2020
@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.
saliksaly commentedon Feb 17, 2020
Hi, would not be there any workaround until 5.0.0 version?
Maybe something similar to DbContextExtensions.ResetValueGenerators in #6872?
ajcvickers commentedon Feb 25, 2020
@saliksaly Just wanted to let you know that I have started investigating a workaround, but it may take me a while.
Force use of common value generator for all derived types
ajcvickers commentedon Mar 6, 2020
@saliksaly I have a PR out to fix this for EF Core 5.0. Here's a workaround for 3.1:
Use it like this:
Passing
2here means that the next value generated will be 3.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.
Force use of common value generator for all derived types
Force use of common value generator for all derived types
Force use of common value generator for all derived types
ShoSashko commentedon May 8, 2020
@ajcvickers
I just wanted to clarify. I have the same issue but with string as a key.
EF Core version: 3.1.0
Database provider: Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET Core 3.1
Please let me know if there is any workaround?
ajcvickers commentedon May 9, 2020
@ShoSashko There is a workaround posted above.
ShoSashko commentedon May 11, 2020
@ajcvickers Thanks for the quick response.
Not sure how I can apply this workaround for my specific case, having a
stringas a PK.I've tried to change the workaround posted above from
inttostringBut it throwing exception of the
generator.Bump.I have doubts that it will work anyway, there is no equal method for
GetIntegerValueGeneratorfor the string.Please let me know if I am even on the right track.
ajcvickers commentedon May 11, 2020
@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.
Applied fix according to dotnet/efcore#19854 (comment)
saliksaly commentedon Jun 11, 2020
I have applied the fix to the demo app:
https://github.com/saliksaly/aspnetcoreapp-efcore-inherited-entity-id-problem
I works now, no problems.
update to EF5 preview 7 because of issue 19854