"Go" entityframework (EF) selection and use of greedy loading and lazy loading

Source: Internet
Author: User

Pardon: http://www.weixq.cn/Article/Detail/906

greedy load: as implies is to read all the things that are going to be loaded at once

1 using (varnew2 3     var  from  in context. Orders.include ("orderdetails"select  o; 4 }

When the order information is read, we want to read the details of the order, and here we use the Include keyword to load the associated table.

Lazy Loading: load (read) when we need to

When we want to view an order information, we want to display its corresponding order detail record, we hope to use lazy loading to achieve, not only to speed up the efficiency of reading, but also to avoid loading unnecessary data. Lazy loading is typically used when the Foreach loop reads data.

So when we're defining the model, we need to add the virtual keyword before the property. As follows

1  Public classOrder2 { 3      Public intOrderID {Get;Set; } 4      Public stringOrdertitle {Get;Set; } 5      Public stringCustomerName {Get;Set; } 6      PublicDateTime TransactionDate {Get;Set; } 7      Public VirtualList<orderdetail> OrderDetails {Get;Set; } 8}

If we want to prohibit the use of lazy loading, the best way is to declare it in the constructor of the DbContext class

1  Public class Mydbcontext:dbcontext 2 {3      Public  45    this         false 6      7 }

Summarize:

Greedy loading:
1. Reduce the latency of data access and return all data in a single database access.
2, one-time reading all the relevant data, may result in the actual use of some data, resulting in slower reading data, inefficient


Lazy Loading:
1. Load only when the associated data needs to be read
2, may reduce performance because of the delay of data access, because in the loop, each data will access the database once, causing the database pressure to increase

To sum up, what kind of mechanism should we use when we should be more clear? My personal advice is to:

1, if the data is loaded in the Foreach loop, then it is better to use lazy loading, because there is no need to read all the data at once, so although it is possible to cause the query of n database, but basically within acceptable range.

2, if at the development time can foresee the need to load all the data at once, including all the data associated with the table, then using greedy loading is a better choice, but this way can lead to efficiency problems, especially in the case of large data volume.

"Go" entityframework (EF) selection and use of greedy loading and lazy loading

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.