primary key constraint updating many-to-many self referential table in entity framework code-first

asp.net-mvc asp.net-web-api entity-framework entity-framework-6 sql-server

Accepted Answer

Identified the issue.

I was eager loading the children using an include line when the entity was first loaded.

I did not eager load the children when I modified the object or when I retrieved it from the database again. As a result, the changes and additions were becoming unclear. Upon included the include during the upload, scenario #2 above was successful (the explicit clear was not needed)

db.Roles.Include("Children").FirstOrDefault(z => z.RoleId == RoleId);

Also related, make sure all the entities in the graph are coming from the same DB context if you see this issue when working with relationships across several tables!

https://msdn.microsoft.com/en-us/magazine/dn166926.aspx

0
4/24/2015 7:02:47 PM

Popular Answer

Before getting data from the database, try disabling auto detect changes via

context.Configuration.AutoDetectChangesEnabled = false;

Then, on the particular role object you are modifying, set the state to modified.

context.Entry(role).State = EntityState.Modified;

I haven't personally done this on a self-referencing many-to-many table, but adding and updating entities in this way can prevent EF from inferring the wrong thing about what you are adding or editing.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow