Let's look at the definition first.
EF Deferred loading : When querying data from an EF entity object using a LAMABDA expression or LINQ, the EF does not query the data directly, but loads it into memory when it is used for specific data.
What object does a where method of an entity object return?
Let's take a look at the code in the previous article to analyze:
#region Query article List +actionresult Article ()
///<summary>
///query article List
///</summary>
///< returns></returns> public
ActionResult Article ()
{
//Get article List db from DB object
. Blogarticles.where (p => P.aisdel = = false);//Use LAMABDA expression to get data for the deleted article/
///LAMABDA expression to retrieve
//return a list< T> object to store article List
< models.blogarticle > list= db. Blogarticles.where (p => P.aisdel = = False). ToList ();
You can also use LINQ to get data
list<models.blogarticle> List1 = (from P in db. Blogarticles where P.aisdel = = False Select P). ToList ();
Use ViewData to pass the list object
viewdata["DataList" = list;
return View ();
}
#endregion
To experience deferred loading, we modify the code as follows:
#region Query article List +actionresult Article ()
///<summary>
///query article List
///</summary>
///< returns></returns> public
ActionResult Index ()
{
//Get article List db from DB object
. Blogarticles.where (p => P.aisdel = = false);//Use LAMABDA expression to get the deleted article
dbquery<models.blogarticle> query = (Db. Blogarticles.where (p => P.aisdel = = False)) as dbquery<models.blogarticle>;
list<models.blogarticle> list = query. ToList ();
Use ViewData to pass the list object
viewdata["DataList" = query;
return View ();
}
#endregion
Why do we use dbquery<t> to receive it here?
First let's look at using DB. Blogarticles.where () to get a list of articles, where () method gives us a return of what type of object? When we put the mouse over the Where () method, we find that the where will return to us a IQueryable generic interface object, as shown in the following figure:
So do we need to use the IQueryable object to receive the object, the code is as follows:
The Where () method returns a IQuery interface IQueryable < models.blogarticle > query= db. Blogarticles.where (p => P.aisdel = = false);
Can query in the end get the value? Let's run the debugger and the results are as follows:
We see in the local variable window that query has fetched the value. But according to the object-oriented principle, the interface can not be directly instantiated, but the code here is not an error, that is why?
Based on the object-oriented Richter substitution principle, we all know that this is actually a subclass object that returns a IQueryable object.
Note: C # on the Richter substitution principle, the subclass object can be assigned to the parent class object. This means that subclasses can replace where the parent class appears. However, the parent class object must not replace the child class object.
That is, the Where () method returns the subclass object of a IQueryable interface and assigns the value to its parent class object IQueryable.
So where () exactly is the object returned (what kind of IQueryable object?)?
Look again at the above bureau variable the return value type of query in the window is as follows:
We can clearly see that the return type of query is Dbquery type.
So we use Dbquery to receive the object, the code is as follows:
Copy Code code as follows:
dbquery<models.blogarticle> query = (db. Blogarticles.where (p => P.aisdel = = False)) as Dbquery<models.blogarticle
Because the Where () method returns the IQueryable object, the object is converted to a Dbquery object.
Delay loading of dbquery<t> generic interface classes
The definition of the EF deferred load is described above, so is there a query that the database performs when we query an object in query?
Here we use SQL Server's own profiler software to view,
① open Sqlserverprofiler Software, a new query, just beginning, is a query record, the following figure:
② we use the above figure, Red arrow refers to the eraser, clear the record, the following figure:
③ We start debugging again, run the code, and when the program runs to the breakpoint, we see that query doesn't have a value, as shown in the following figure:
④ Single Step debugging, the next step, we found in the local Variable window query has been taken to the value, the following figure:
The value of query, as shown in the following figure:
So what happened to our Sqlserverprofiler tracker? No changes have taken place, as shown below:
If we use the Ado.net operation database, after querying the data, the data will be sent to the receiving object (for example, a DataTable object), but how does the EF operation database not immediately query the data?
Deferred loading of ⑤dbquery objects
When we go back to querying the database when we use the query object, we proceed to the next step, and the results are as follows:
This time the list takes the value, and then SQL Server Profiler also changes, with a record of querying the database, as shown in the following figure:
In other words, when the query Object ToList (), the operation of querying the database occurs.
Third, summary
(1) The Dbquery object in the EF has a delay load when it operates the database, and it is not used to accept objects directly when using List<t>;
2 delay loading does not immediately query the database, but only when the data is used the EF will query the database.
The above is about the ASP.net MVC development process of the EF delay loading related introduction, hope to help everyone's study.