问题
I'm working with Entity Framework 6 Code First and Fluent API. I have a one to many relationship between Visit and VisitPage (1 Visit has many VisitPage objects). This is the simplified version of the POCO classes:
Visit: Id (identity), UrlReferrer, Pages. VisitPage: Id (identity), Name, Visit, VisitId
First, I create a Visit object and add a VisitPage object to it. Then, I insert them into the DB. Until this point everything works OK. Observation: VisitId property in VisitPage is automatically set after insertion (it takes the identity value from DB and sets the property).
Then, in a disconnected environment, I add a new page to the visit (I don't get the Visit object from the DB, that's what I mean by disconnected environment). This new page is referring the same visit object but I'm not setting the VisitId property because I think that EF should set it based on the Visit property. And that's the problem, EF doesn't set it and, in fact, it throws an exception saying that the value of Visit property and VisitId property doesn't match. I was expecting that EF was going to be able to do this under the hood (in the DetectChanges method for example) but it doesn't.
Exception message:
A referential integrity constraint violation occurred: The property value(s) of 'VisitId' on one end of a relationship do not match the property value(s) of 'VisitPage.VisitId' on the other end.
So, how do I solve this? I see two possible solutions: 1) Work in a connected environment --> get the Visit object from EF and then add the page object. In this case EF updates VisitId value and it works. 2) Set VisitId manually after setting the Visit object.
I hope I've been clear enough. If not, please let me know.
回答1:
Either do this
visitPage.VisitId = VisitPage.Visit.Id;
visitPage.Visit = null;
or
context.Visits.Attach(visitPage.Visit);
or
context.Entry(visitPage.Visit).State = EntityState.Unchanged;
来源:https://stackoverflow.com/questions/25026542/entity-framework-disconnected-graph-and-navigation-property