Skip to content
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

Problem with N-M entites #160

Open
edi0177 opened this issue May 4, 2017 · 0 comments
Open

Problem with N-M entites #160

edi0177 opened this issue May 4, 2017 · 0 comments

Comments

@edi0177
Copy link
Contributor

edi0177 commented May 4, 2017

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant