Differences between Attach and Entry in mvc ef, mvcefattachentry

Source: Internet
Author: User

Differences between Attach and Entry in mvc ef, mvcefattachentry

EF: three methods for adding a container --> query, attach, entry. is not in the container. remove, add, and other methods cannot be used.

1.0 Using Attach may cause errors.// Attach;
Var db = new PhoneBookEntities ();
Var queryCi = db. ContactInfo. FirstOrDefault (c => c. ID = 10); // make the EF container have a proxy class and the status is Unchanged
ContactInfo ci = new ContactInfo () {ID = 10 };
Db. ContactInfo. Attach (ci );
An object of the append type "MyMvc. Models. ContactInfo" fails because other objects of the same type already have the same primary key value.
The proxy class of the object already exists in the container, but the Attach method must set the status of the proxy class to Unchanged, which may conflict with the status of the original proxy class, so an error is reported.

Var db = new PhoneBookEntities ();
Var queryCi = db. ContactInfo. FirstOrDefault (c => c. ID = 10 );
Db. ContactInfo. Attach (queryCi); // The status of the original proxy class is Unchanged. The Attach method also sets the status of the proxy class to Unchanged without conflict.
No error will be reported for this writing, but it is meaningless.

2.0 the Entry method returns the proxy class of the object, and the Attach method returns the object.
ContactInfo c1 = db. ContactInfo. FirstOrDefault (c => c. ID = 10 );
DbEntityEntry <ContactInfo> x1 = db. Entry (c1 );
ContactInfo x2 = db. ContactInfo. Attach (c1 );

3.0 if an object already has a proxy class in the EF containerThe Entry method does not return an error. The proxy class is returned and the state remains unchanged.
If state = Unchanged before using the Entry method, the returned proxy class state is also Unchanged;
If state = Deleted before using the Entry method, the returned proxy class state is also Deleted.
Var db = new PhoneBookEntities ();
ContactInfo ci = db. ContactInfo. FirstOrDefault (c => c. ID = 10 );
Var c1 = db. ContactInfo. Remove (ci );
String sC1 = db. Entry (ci). State. ToString (); // Deleted
Var c2 = db. ContactInfo. Attach (ci );
String sc2 = db. Entry (ci). State. ToString (); // The Attach method changes the agent class status from Deleted to Unchanged.

Summary: to avoid errors when using the Attach method, try to use the Attach method or the existing objects in the container that has been queried by Attach as little as possible.
4.0 if an object does not exist in the containerThe status of the proxy class obtained by the Entry method is Detached.
After the Attach method, the status of the proxy class changes to Unchanged.
Var db = new PhoneBookEntities ();
Var ci = new ContactInfo () {ID = 10 };
Var state = db. Entry (ci). State. ToString (); // Detached: This state indicates that db. SaveChanges (); does not affect this object.
Db. ContactInfo. Attach (ci );
State = db. Entry (ci). State. ToString (); // Unchanged

The Detached status indicates that the EF container does not track changes in the object, and the EF performance is better.

When will it be used? Used to display data.

Usage, such as db. ContactInfo. AsNoTracking (). Where (c => c. ID> 2 );

 

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.