Entity Framework Tip 4-how to use notracking to query entities in the detached state? [Reprinted]

Source: Internet
Author: User
Entity
Framework Tip 4-how to use notracking to query objects in the detached state?

Sometimes our entities only need to be displayed without updating, so to improve performance, we do not need to be
Context tracing. In this case, you can use the notracking query to obtain the object, so that the object state will be detached.

 

In ef3.5 SP1 and EF 4, we can perform notracking queries as follows:

Using (VAR context = new myobjectcontext ())
{
Context. People. mergeoption = System. Data. Objects. mergeoption. notracking;
VaR list = context. people. Where (P = & gt; personid> 100). tolist ();
}

You can also use the executestorequery API to directly call SQL commands to obtain objects:

Using (VAR context = new myobjectcontext ())
{
VaR query = context. executestorequery <parent> ("select * from parent where parent. parentid> @ ID", "testdbentities. Parents ", System. Data. Objects. mergeoption. notracking, New sqlparameter ("@ ID", 100 ));
}

Or use the load method to obtain the relevant entity:

Using (VAR context = new myobjectcontext ())
{
Context. contextoptions. lazyloadingenabled = false;
VaR person = context. People. First ();

  Person. Courses. Load (system. Data. Objects. mergeoption. notracking );
}

In EF 4.1, we can use the newly added asnotracking method:

Using (VAR context = new mydbcontext ())
{
VaR people = context. people. Where (P => P. personid> 100 ). Asnotracking(). Tolist ();
}

 

The asnotracking method calls objectquery. mergeoption in the past to implement notracking queries. This method (dbhelpers. createnotrackingquery) is the core of the asnotracking method:

Public static iqueryable createnotrackingquery (objectquery query)
{
Iqueryable queryable = query;
Objectquery query2 = (objectquery) queryable. provider. createquery (queryable. expression );
Query2.mergeoption = Mergeoption. notracking;
Return query2;
}

 

We will introduce a small problem about the detached entity obtained by notracking. The entity obtained by notracking query is different from the entity obtained by calling the detach method directly. The former retains a reference to the current context, so that the load method can be called to explain
Load-related entities. The latter is completely out of the corresponding context, so it belongs to the real detached state. Careful readers may think that notracking has
The detached object will cause the context to be referenced all the time, and thus cannot be promptly spam (GC ). Indeed, this is confirmed by the product group
Design. If context is not referenced, explicit it load cannot be supported.

Sometimes our entities only need to be displayed without updating, so to improve performance, we do not need to be
Context tracing. In this case, you can use the notracking query to obtain the object, so that the object state will be detached.

 

In ef3.5 SP1 and EF 4, we can perform notracking queries as follows:

Using (VAR context = new myobjectcontext ())
{
Context. People. mergeoption = System. Data. Objects. mergeoption. notracking;
VaR list = context. people. Where (P = & gt; personid> 100). tolist ();
}

You can also use the executestorequery API to directly call SQL commands to obtain objects:

Using (VAR context = new myobjectcontext ())
{
VaR query = context. executestorequery <parent> ("select * from parent where parent. parentid> @ ID", "testdbentities. Parents ", System. Data. Objects. mergeoption. notracking, New sqlparameter ("@ ID", 100 ));
}

Or use the load method to obtain the relevant entity:

Using (VAR context = new myobjectcontext ())
{
Context. contextoptions. lazyloadingenabled = false;
VaR person = context. People. First ();

  Person. Courses. Load (system. Data. Objects. mergeoption. notracking );
}

In EF 4.1, we can use the newly added asnotracking method:

Using (VAR context = new mydbcontext ())
{
VaR people = context. people. Where (P => P. personid> 100 ). Asnotracking(). Tolist ();
}

 

The asnotracking method calls objectquery. mergeoption in the past to implement notracking queries. This method (dbhelpers. createnotrackingquery) is the core of the asnotracking method:

Public static iqueryable createnotrackingquery (objectquery query)
{
Iqueryable queryable = query;
Objectquery query2 = (objectquery) queryable. provider. createquery (queryable. expression );
Query2.mergeoption = Mergeoption. notracking;
Return query2;
}

 

We will introduce a small problem about the detached entity obtained by notracking. The entity obtained by notracking query is different from the entity obtained by calling the detach method directly. The former retains a reference to the current context, so that the load method can be called to explain
Load-related entities. The latter is completely out of the corresponding context, so it belongs to the real detached state. Careful readers may think that notracking has
The detached object will cause the context to be referenced all the time, and thus cannot be promptly spam (GC ). Indeed, this is confirmed by the product group
Design. If context is not referenced, explicit it load cannot be supported.

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.