1

I have a db-first Entity Framework application and the following associations:

Customer * <-> 1 Country
Machine * <-> 1 Customer

Everything is fine until now. Now here is the problem:
I have another class Condition associated with a machine as well as with a customer:

Condition * <-> 1 Customer
Condition * <-> 1 Machine

In one special entity Condition.Machine.Customer.Country is the same entity as Condition.Customer.Country and i get an InvalidOperationException with the message

An object with the same key already exists in the ObjectStateManager

This exception appears when i call

db.Entry(condition).State = EntityState.Modified;

Also the Country Entites are unchanged i get this Exception.

How can i now store the Condition Entity?

Obl Tobl
  • 5,604
  • 8
  • 41
  • 65
  • Is this in a disconnected context? i.e. a context only exists for the query, and is disposed, and when calling save changes a new context is created? – Mashton Mar 25 '14 at 14:17
  • It is a WCF and a new context is created with every call to the database. It also works with every other object, but in this special case i have two identical Country-Entities and i get this Exception. So there is no problem with the context. – Obl Tobl Mar 25 '14 at 14:19
  • I'm not saying it is a problem with your context, but how you are using your context is important for how you save your changes. – Mashton Mar 25 '14 at 14:31
  • I guess, there is possible loop design issue in your database. I would suggest you to check http://stackoverflow.com/questions/8115748/why-should-i-avoid-loops-when-designing-relationships-for-a-database – serhads Mar 25 '14 at 14:35
  • @serhads I already checked it, there are no loops – Obl Tobl Mar 25 '14 at 14:36
  • @OblTobl Is it possible to include the entities in your question? – Leron Mar 25 '14 at 14:43
  • @Leron what exactly do you want to know? I think all relevant associations are mentioned, no? – Obl Tobl Mar 25 '14 at 14:47
  • @Leron It is a disconnected context, one-to-one/many graph update problem. EF can't track the state of children in a disconnected context unless you, as a developer, help it. – Mashton Mar 25 '14 at 14:51
  • @OblTobl I had relatively similar problem and the solution was in the way the entities were created. I'm not telling you that your case is the same, if you can/want post the code, if not - no problem. – Leron Mar 25 '14 at 14:56

1 Answers1

0

If you are working in a disconnected context you can't rely on the EF TrackChanges mechanism, and with graph relationships the context you are saving in has no way of knowing whether the child is new/updated/no-change. When you come to save changes you will get the error you're seeing because that child entity already exists and EF is attempting to add it again.

Julie Lerman has a method that involves managing your own modified state, so that you can correctly attach/detach the entities as needed.

Another method is shown here: http://www.entityframeworktutorial.net/EntityFramework4.3/update-one-to-one-entity-using-dbcontext.aspx

This reloads the child entity you are trying to save (without tracking), removes it from the parent, and then allows you to mark entity state of parent as 'modified' and re-add the child if it has one.

Mashton
  • 6,037
  • 2
  • 25
  • 35
  • The state of the Country-Entities is Unchanged, furthermore EntityState.Modified is something different than EntityState.Added. With EntityState.Modified nothing is added again like you mentioned. – Obl Tobl Mar 25 '14 at 14:39
  • Have you inspected the state of Country at the time that you change the state of Condition? – Mashton Mar 25 '14 at 14:41
  • One more thing: You mentioned Julie Lerman. In her answer to this question she also mentions that subobjects are marked as unchanged: http://thedatafarm.com/data-access/email-qa-entity-framework-modified-state-and-savechanges/ . So your answer is just wrong that setting the parent to modified also sets the children to modified! – Obl Tobl Mar 25 '14 at 14:42
  • Listen, I'm just trying to help you. You haven't run into these problems when trying to save disconnected graphs before: they are complicated and you are not the only person who has had problems. I was pointing you in the right direction (well 2). You can nitpick about my wording if you like, but that won't solve your problem. EF is attempting to insert your child because it can't track its state, and doesn't know it isn't a new entity. THIS is causing your error. THAT is what you need to fix, and there are a few ways of doing that. http://msdn.microsoft.com/en-us/magazine/dn166926.aspx – Mashton Mar 25 '14 at 14:48
  • Another solution, similar to Julies custom State interface so that your entities can track their own state: http://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/ then on server you can inspect this to work out what EntityStates the parts of your graph should have. – Mashton Mar 25 '14 at 15:08