The perfect solution for the error reported by Attached in Entity Framework

Source: Internet
Author: User

The perfect solution for the error reported by Attached in Entity Framework

I have published an article entitled "perfect solution for errors reported by Attached in Entity Framework", which can solve the errors reported by Attached when a single object is updated or deleted, note that a single object here refers to an object to be updated or deleted that does not contain other entities (such as navigation attributes that contain other entities), that is, a simple POCO object; but what if not? The method in that article does not work to a certain extent, and an error will still be reported. I cannot understand it at first. It is clear that the entity to be updated is not Attached through the IsAttached function, however, during Attaching, the system still reports an error saying that the same Key exists. At first, we thought it was a ms bug. After repeated debugging, we found that the error was correct, the error is not the object to be updated, but the object associated with the object. The error is as follows: (only the Demo code is used)

Public class A {public string a {get; set;} public string B {get; set;} public string c {get; set;} public virtual B {get; set ;}} public class B {public string x {get; set ;}public string y {get; set ;}public string z {get; set ;}} var a1 = dbContext. set <A> (). single (); a1.a = "test1"; dbContext. saveChanges (); dbContext. detach (a1); // remove the a1 entity from the cache; var a2 = dbContext. set <A> (). asNoTracking (). single (); a2.a = "test2"; dbContext. set <A> (). attach (a2); // an error is reported, indicating that the same KEY of B already has AttacheddbContext. entry (entity ). state = EntityState. modified; dbContext. saveChanges ();

In response to this error, I was wondering why object A can be associated with object B and Attached to the memory at the same time. When I execute Detach object, but it cannot be associated with the Detach entity B. The root cause of the problem is here. Now we know the reason. How can we solve this problem? Since we know that the Detach object is incomplete, we only need to obtain all existing Attached objects in the current DbContext context object. When the corresponding CRUD is executed, then Detach all the data in sequence. The solution code is as follows:

/// <Summary> /// clear all cached object objects in the DB context // </summary> private void DetachedAllEntities () {var objectContext = (IObjectContextAdapter) this. baseContext ). objectContext; List <ObjectStateEntry> entries = new List <ObjectStateEntry> (); var states = new [] {EntityState. added, EntityState. deleted, EntityState. modified, EntityState. unchanged}; foreach (var state in states) {entries. addRange (objectContext. objectStateManager. getObjectStateEntries (state);} foreach (var item in entries) {objectContext. detach (item. entity) ;}} public void Commit () // encapsulated unified Commit method {this. baseContext. saveChanges (); this. detachedAllEntities (); // clear}

When using the IsAttached function in the previous article, we can solve all the Attached errors perfectly!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.