Essence of Entity FrameWork delayed loading (1), entityframework

Source: Internet
Author: User

Essence of Entity FrameWork delayed loading (1), entityframework

1. The standard query operator method of the set is an extension method added to the IEnumerable interface in System. Linq. Enumerable.

2. The standard query operator method in DBSet <T> in EF context is derived from the extension method added by the IQueryable interface in System. Linq. Queryable.

3. one of the essential reasons for delayed loading: currently, multiple standard query operator methods may be used to combine query conditions. Therefore, each method only adds a query field and cannot determine the query conditions, whether the SQL statement has been added or not. Therefore, it is impossible to determine what the SQL statement is when each standard query operator method is used. Only one DbQuery object containing all the conditions for adding can be returned; when this DbQuery object is used, SQL statements are generated based on all conditions to query the database.

The following is the code for instant query in disguise:

1 # region Query + void Query () 2 // <summary> 3 // Query 4 /// </summary> 5 static void Query () 6 {7 // query data named Andy Lau. 8 // Real-Time query in disguise (you can use the SQL Server Profiler tool to listen. After executing this sentence, you can immediately query the database, an SQL statement is generated, which is an instant query.) 9 List <Studnet> stu = db. studnets. where (s => s. s_Name = "Andy Lau "). toList (); 10 11 // use the foreach method of the set to traverse the queried data 12 // the query result is an object, which is similar to system. what. This is because the ToString () method is the Object method. here we need to rewrite a Tostring method for the Student Object. 13 // because the T4 template will automatically overwrite the previous changes and regenerate each time it is saved. Therefore, you need to write a partial category for student and overwrite the Tostring () method 14 stu in the partial category. forEach (s => Console. writeLine (s. toString (); 15} 16 # endregionQuery

 

The latency query is as follows:

 

 

The real delayed loading is the DbQuery class, which implements the IQueryable interface, as shown in the figure:

What is delayed loading?

 

See:

 

1 // latency query 2 DbQuery <Studnet> I = db. studnets. where (s => s. s_Name = "") as DbQuery <Studnet>; // after this statement is executed, no SQL statement is generated immediately to query the database. 3 4 Studnet st = I. firstOrDefault (); // The database is queried only when used to generate an SQL statement (you can use the tool provided by SQL Server, which is listened by SQL Server Profiler)

 

Related Article

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.