EF's attach Approach

Source: Internet
Author: User



Before introducing the Attach method, the knowledge points related to it are introduced first.

Attach method: Attaches the given entity to the context in System.Data.EntityState.Unchanged state

It can be seen from the explanation that the main purpose of the Attach method is to attach an object that is not tracked by DbContext to the dbcotext so that it is DbContext tracked

1 Object Context: DBContext A new context instance is created to create the name of the database to connect to, and the default state is that no objects are tracked

2 Entity Status: There are 4 states for the entity in EF:

2.1 Added: Object is a new object and has been added to the object context, but has not yet been called

2.2 Deleted: Object has been removed from the object context

2.3 Detached: The object exists but is not tracked. The entity is in this state after the entity is created, but before it is added to the object context

2.4 Modified: A scalar property on an object has changed, but has not yet been called

2.5 Unchanged: This object has not been modified since the object was attached to the context, or since the last call (all objects have been changed to unchanged state after the SaveChange method has been called)

Having learned the relevant knowledge, we began to use the Attach method to change the code.

The above is the original method

Copy Code
using (Entities CTX = new entities ())
{
Product Product = CTX. Product.first ();
Update Property Action
CTx. SaveChange ();
  
}
Copy Code
This type of writing generates two operations on the database and is changed to the Attach method as follows

Copy Code
public void Update (product product)
{

using (Entities CTX = new entities)
{
After product has been updated by the foreground
CTx. Attach (product);
CTx. Objectstatemanager.changeobjectstate (entity,entitystate.modified)
CTx. SaveChange ();
}
}
EF is handled as follows
1 attaching the object to the context and changing the state to the modified state
2 An update SQL statement is generated when the SaveChange method is called and the Where condition
Is the primary key ID for the object, because EF updates and deletes are handled based on the primary key ID.
Copy Code
The delete operation is the same, this is only used to paste the attach processing method.

Copy Code
public void Delete (product product)
{

using (Entities CTX = new entities)
{
Product entity = new Product{id = 1}
CTx. Attach (entity); CTx. Objectstatemanager.changeobjectstate (entity,entitystate.deleted)
CTx. SaveChange ();
}
}
It says that EF is handled according to the primary key ID, so it can be deleted by manually generating an object and assigning the corresponding ID to the attach in the context.
Copy Code


Compared to the original method in the project, with attach after the operation of the database is reduced once, the performance will be greatly improved!

EF's attach Approach

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.