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.