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
The class ExpenseType contains static readonly objects that represents all possible types. The domain logic depends on them and shouldn't know about DbContext. All of ExpenseTypes are stored in database using HasData. Before saving i ensure that all entites of type ExpenseType have Unchanged state.
Both ExpenseType and Expense have overridden Equals, GetHashCode methods but implementation of StateManager's private dictionaries compare references.
Is there another way to overcome this problem.
Exception message:
The instance of entity type 'ExpenseType' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Stack Trace:
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.ThrowIdentityConflict(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node, Boolean force)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.NavigationReferenceChanged(InternalEntityEntry entry, INavigation navigation, Object oldValue, Object newValue)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.NavigationReferenceChanged(InternalEntityEntry entry, INavigation navigation, Object oldValue, Object newValue)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectNavigationChange(InternalEntityEntry entry, INavigation navigation)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(IStateManager stateManager)
at Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.DetectChanges()
at Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.Entries[TEntity]()
at EfCoreProblemWithDDD.ExampleDbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) in C:\Workspace\_experiments\EfCoreProblemWithDDD\src\EfCoreProblemWithDDD\ExampleDbContext.cs:line 18
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
at EfCoreProblemWithDDD.Tests.Tests.Test1() in C:\Workspace\_experiments\EfCoreProblemWithDDD\test\EfCoreProblemWithDDD.Tests\Tests.cs:line 40
EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio Professional 2017 15.9.15
The text was updated successfully, but these errors were encountered:
The class
ExpenseType
contains static readonly objects that represents all possible types. The domain logic depends on them and shouldn't know about DbContext. All of ExpenseTypes are stored in database usingHasData
. Before saving i ensure that all entites of typeExpenseType
haveUnchanged
state.Both
ExpenseType
andExpense
have overriddenEquals
,GetHashCode
methods but implementation of StateManager's private dictionaries compare references.Is there another way to overcome this problem.
Steps to reproduce
I have created repo that contains minimal working example showing my problem:
https://github.com/bopazyn/EfCoreProblemWithDDD
Further technical details
EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio Professional 2017 15.9.15
The text was updated successfully, but these errors were encountered: