To better understand the structure of an entity, think of DbContext as a proxy between your application and the database. DbContext will cache everything and will use every bit of data from the cached values ββif you do not tell it about it.
For 1 .. It depends on your environment, if your DbContext not located between selecting and updating entites, you can simply call SaveChanges and your data will be saved. If your DbContext configured, you can detach the entites from the context, modify the data, reconnect it, and set the EntityState to change.
I cannot give you a 100% answer because I stopped using the entity structure about six months ago. But I know that itβs painful to update complex relationships.
For 2 .: the AsNoTracking team tells EF not to track changes made to entities within this request. For example, you select 5 temporary tables from your database, change some values ββin the first object and delete the last one. DbContext knows that the first object is changed, and the last is deleted, if you call SaveChanges , DbContext will automatically update the first object, delete the last one and leave the others untouched. Now you are trying to update the object yourself and reattach the first object to the DbContext.
Now DbContext has two sides with the same key, and this will result in your exception.
For 3 .: DynamicProxies is an object that the entity infrastructure uses to track changes to these objects.
For 4 .: check the link , there is also a good book about the entity framework 6 (Title: "Programming the entity structure")
JoeJoe87577
source share