LINQ to SQL series three-delay Loading

Source: Internet
Author: User

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

We can find that the relationship between tstudent and tclass is one-to-one (two tables are required), while that between tstudent and tcourse is multi-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:

Public static void deferrredloading () {using (l2sdbdatacontext DB = new l2sdbdatacontext () {// output log on console dB. log = console. out; dB. deferredloadingenabled = true; // default value: True // get a student tstudent student = dB. tstudents. firstordefault (); // access property tclass of tstudent console. writeline ("{0} belongs to {1}", 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 to load 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 select its tclass using another SQL statement. The Code is as follows:

Public static void loadwith () {using (l2sdbdatacontext DB = new l2sdbdatacontext () {// output log on console dB. log = console. out; var loadoptions = new dataloadoptions (); // you can specify the table loadoptions to be loaded when you load tstudent. loadwith <tstudent> (P => P. tclass); dB. loadoptions = loadoptions; // get a student tstudent student = dB. tstudents. firstordefault (); // access property tclass of tstudent console. writeline ("{0} belongs to {1}", student. name, student. tclass. classname );}}

Output results. Observe the generated SQL statements.

This generation generates an SQL statement that uses inner join.

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.