LINQtoSql series three-delay Loading

Source: Internet
Author: User
LINQtoSql Series 3 delayed loading first, let's look at the relationship diagram of the data table: From this, we can find that the relationship between TStudent and TClass is one-to-one (two tables are required ), the relationship between TStudent and TCourse is many-to-many (three tables are required ). DeferredLoadingEnabledDataContext's DeferredLoadingEn

First of all, let's take a look at the relationship diagram of the data table during the three-delay loading of the LINQ to SQL series: From this, we can find that the relationship between TStudent and TClass is one-to-one (two tables are required ), the relationship between TStudent and TCourse is many-to-many (three tables are required ). DeferredLoadingEnabled DeferredLoadingEn of DataContext

LINQ to SQL series three-delay Loading

First, let's take a look at the relationship diagram of the data table:

From the Hong Kong space, we can find that the relationship between TStudent and TClass is one-to-one (two tables are required ), the relationship between TStudent and TCourse is many-to-many (three tables are required ).

DeferredLoadingEnabled

The DeferredLoadingEnabled attribute of DataContext indicates whether delayed loading is required. The default value is true. Take TStudent as an example. The delayed loading objects refer to TClass and the corresponding TCourse. When the value of delayed loading is set to true, when the TClass attribute or TStudentCourse attribute of the TStudent instance is accessed, the data in the TClass table and the TStudentCourse table is automatically loaded, as shown in the following sample code:

DeferrredLoading () {using (L2SDBDataContext db = new L2SDBDataContext () {// output log on consoledb. log = Console. out; db. deferredLoadingEnabled = true; // default value: true // get a studentTStudent student = db. TStudents. firstOrDefault (); // access property TClass of TStudentConsole. writeLine (, student. name, student. TClass. className );}}

The Code shows that when the DeferredLoadingEnabled of DataContext is true, data in the relational table can be directly accessed. For example, student. TClass.

Output result:

From the results, we can see that DataContext uses two SQL segments, the Hong Kong server, loading data from TStudent and TClass respectively. If the DeferredLoadingEnabled of DataContext is set to false, the data accessing the relational table will throw an exception of null reference.

LoadWith

LoadWith is not a DataContext method, but a DataLoadOptions method. You can set the LoadOptions attribute for DataContext to change the way DataContext loads data; in other words, LoadOptions sets whether to use join to load the associated table data when the select primary table data.

For example, when I want to select a TStudent table, I also use another SQL statement to select its TClass. The Hong Kong server has the following code:

LoadWith () {using (L2SDBDataContext db = new L2SDBDataContext () {// output log on consoledb. log = Console. out; var loadoptions = new DataLoadOptions (); // you can specify the table loadoptions to be loaded when you load TStudent. loadWith (P => p. TClass); db. loadOptions = loadoptions; // obtain a studentTStudent student = db. TStudents. firstOrDefault (); // access property TClass of TStudentConsole. writeLine (, student. name, student. TClass. className );}}

Output results. Observe the generated SQL statements.

This generation generates an SQL statement that uses inner join.

Posted on

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.