Entity FrameWork delay loading nature (2), entityframework

Source: Internet
Author: User

Entity FrameWork delay loading nature (2), entityframework

1. For foreign key entities, EF will query the corresponding table only when this foreign key attribute is used. This is loaded as needed...

2. disadvantages of On-Demand Loading: the database will be queried every time the foreign key entity is called (EF has minor optimization: the same foreign key entity is queried only once)

1             IQueryable<StudentDetail> sd=db.StudentDetails;3             foreach (var item in sd)4             {5                Console.WriteLine(item.Studnet.s_Name);6             }

The solution is the same as when we normally use databases. We use inner jion for queries. To generate such a statement in EF;

Db. Students. Include ("c_ID"); --> Syntax: instance of the data context object. Table name. Include ("foreign key name ");

You can use the Include () method to tell EF which foreign key attribute to query by link. When you have multiple foreign key attributes, you can use multiple Include () Methods to query links. Db. Students. Include ("c_ID"). Include ("xxx"). Include ("sss ");.....

IQueryable <StudentDetail> sd = db. studentDetails. include ("Studnet"); // After the code is executed, SQL statements are not generated immediately to execute the query, that is, delayed loading. (delayed loading is supported only when the IQueryable interface is available ), the returned results of a query are of the IQueryable type. Therefore, you can use the DbQuery <Studentdetail> subclass to receive the results. Delayed loading is implemented through the DbQuery class.

// DbQuery <StudentDetail> sd = db. StudentDetails. Include ("Studnet ");

Foreach (var item in sd ){
// When you access the foreign key entity in the StudentDetail object, EF queries the Student table corresponding to StudentDetail. After the query, it loads the data. This foreign key entity
Console. WriteLine (item. Studnet. s_Name); // when this sentence is executed, the generated SQL (including the Inner join SQL) is executed and the query is executed. It is generated only once, and will not be generated when the query is performed cyclically. In this way, the external creation delay loading is solved. }

In this way, the inner join SQL statement is automatically generated when the program is running. When we use multiple foreign key attributes, we will not query the foreign key table any more.

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.