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
I added with #159 a test-case to describe the issue i'am facing with N-M Entities.
This is a simple example of my Use-Case.
I try to describe it in the northwind-shema.
Let's say i have a List of Employees.
And I have a List of Territories.
So I want to remove one Territory from one employee and add the same Territory to another employee.
But the Territory-Object is in both cases not the same, it can't be the same because one is in trackingState.Added and the other one in deleted.
Having this in object-graph leads to the
System.InvalidOperationException: Attaching an entity of type 'TrackableEntities.EF.Tests.NorthwindModels.Territory' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
because the Entity is already Attached to the Context
Exception comes from SetEntityState
// Set state normally if we cannot perform interception if (interceptors == null) { context.Entry(item).State = state; <-- This fails if a other Territory with same id is already attached return; }
My usecase is a little bit more complex but in short this is what i facing.
My Question is how to solve this or is this (maybe unwanted behavior ) in ApplyChanges and if we should maybe check if the entity is already attached and in State unchanged and if so to decide to not set the State again.
The text was updated successfully, but these errors were encountered:
Hello TE contributors,
I added with #159 a test-case to describe the issue i'am facing with N-M Entities.
This is a simple example of my Use-Case.
I try to describe it in the northwind-shema.
Let's say i have a List of Employees.
And I have a List of Territories.
So I want to remove one Territory from one employee and add the same Territory to another employee.
But the Territory-Object is in both cases not the same, it can't be the same because one is in trackingState.Added and the other one in deleted.
Having this in object-graph leads to the
because the Entity is already Attached to the Context
Exception comes from SetEntityState
// Set state normally if we cannot perform interception if (interceptors == null) { context.Entry(item).State = state; <-- This fails if a other Territory with same id is already attached return; }
My usecase is a little bit more complex but in short this is what i facing.
My Question is how to solve this or is this (maybe unwanted behavior ) in ApplyChanges and if we should maybe check if the entity is already attached and in State unchanged and if so to decide to not set the State again.
The text was updated successfully, but these errors were encountered: