1

I have lately updated from EntityFramework 4.3.1 (with Self Tracking) to EntityFramework 6.1.2 (without Detect Changes). Due to having an MVC client with complex entity tree views, and some other technological choices (like using SQL Server's RowVersion) I work in front of the database in detached mode. I have a new instance of the DbContext with every request, and I mark the context's state for each entity.

It works just fine. Insert is working on the complete entity tree. Update is working on the type being handled. On EntityFramework 4 I had to null all navigation properties to have that flow working. Now I don't seem to have to do that.

BUT, I do get the above exception in the following scenario:

  1. I have a parent entity node, say Person, that has been modified;
  2. it contains more than one child entity node of the same type, say Phones, which are new entries, and thus their identity values are identical (0 for new);

Setting the state of the Person's entry to modified causes the exception, although I would expect EntityFramework to not try to handle new (added) child entities' state. I can see when I have only one new phone under the modified Person, that the state of the Phone entry goes from detached to unchanged.

Do I need to do something so EntityFramework knows to handle only the parent entity without its navigation properties on modifications? Or must I resolve to nulling all navigations as I did before?

user3058082
  • 203
  • 1
  • 2
  • 7
  • Please have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:26

0 Answers0