Haha, since I learned EF, how can I not involve the delayed loading feature of EF! So what is delayed loading of EF? Let's take a look at this article.
EF delayed loading: When the Lamabda expression or Linq is used to query data from the EF object, EF does not directly query the data, instead, it loads the data to the memory.
1. What object does the Where method of an object return?
Let's take a look at the code of the previous article for analysis:
Region Query Article list + ActionResult Article ()////// Query the document list //////
Public ActionResult Article () {// get the Article list db through the db object. blogArticles. where (p => p. AIsDel = false); // use the Lamabda expression to obtain the deleted article // use the Lamabda expression to obtain data // return a List
Object To store the article List <Models. blogArticle> list = db. blogArticles. where (p => p. AIsDel = false ). toList (); // you can also use Linq to obtain the data List.
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 delayed loading, we modify the Code as follows:
# Region Query Article list + ActionResult Article ()////// Query the document list //////
Public ActionResult Index () {// obtain the list of articles db through the db object. blogArticles. where (p => p. AIsDel = false); // use the Lamabda expression to obtain the deleted article DbQuery.
Query = (db. BlogArticles. Where (p => p. AIsDel = false) as DbQuery
; List
List = query. ToList (); // use ViewData to pass the list object ViewData [DataList] = query; return View () ;}# endregion
Why do we use DbQuery? To receive it?
First, let's take a look at the type of object that the Where () method returns to us when db. BlogArticles. Where () is used to retrieve the article list? After we move the cursor over the Where () method, we will find that Where will return an IQueryable generic interface object, for example:
Do we need to use the IQueryable object to receive the obtained object? The Code is as follows:
// The where () method returns an IQuery interface IQueryable <Models. BlogArticle> query = db. BlogArticles. Where (p => p. AIsDel = false );
Can the Query here obtain the value? Run the debugging program. The result is as follows:
We can see in the local variable window that the Query has obtained the value. But according to the object-oriented principle, the interface cannot be directly instantiated, but the code here does not report an error. Why?
We all know that the IQueryable object is actually a subclass object based on the Rits replacement principle of object-oriented objects.
Note: the C # Lishi replacement principle allows subclass objects to be assigned to parent objects. That is to say, the Child class can replace the place where the parent class appears. However, the parent class object cannot replace the subclass object.
That is to say, the Where () method returns a subclass object of the IQueryable interface, and assigns a value to its parent class Object IQueryable.
So what kind of object is returned by Where () (what kind of IQueryable subclass object )?
The Return Value Type of query in the preceding Variable Window is as follows:
We can clearly see that the return type of a query is DbQuery.
Then we use DbQuery to receive objects. The Code is as follows:
DbQuery
query = (db.BlogArticles.Where(p => p.AIsDel == false)) as DbQuery
;
Because the Where () method returns an IQueryable object, you need to convert the object into a DbQuery object;
2. DbQuery Delayed loading of generic interface classes
The definition above has already mentioned the definition of EF delayed loading. In this case, when we query the object, does the database perform the query operation?
Here we use SQL Server's own Profiler software to view,
① Open the SQL Server Profiler software and create a new query. At the beginning, there are query records, such:
② In use, the Red Arrow refers to the rubber, clear the record, such:
③ We start debugging again and run the code. When the program runs to a breakpoint, we can see that the query has no value, such:
④ Perform one-step debugging and perform the next step. In the local variable window, we find that the query has obtained the value, for example:
Query value, for example:
So what changes have taken place in our SQL Server Profiler tracker? No changes have occurred, such:
If we use ADO. NET Operation database. After the data is queried, the data is immediately sent to the received object (such as the able object). But how does EF operation database not query the data immediately?
⑤ Delayed loading of DbQuery objects
When we use a query object, we can go back to query the database. The result is as follows:
At this time, the list value is obtained, and the SQL Server Profiler is also changed, with the database query records, such:
That is to say, the database query operation occurs only when the query object ToList () is used.
Iii. Summary:
1) when the DbQuery object in EF is used to operate the database, loading is delayed, and the List To accept objects;
2) Delayed loading does not query the database immediately, but does not query the database only when data is used.
Graduation internship exchange group: 221376964. You can also pay attention to my Sina Weibo.
?? ??