Introduction to data loading methods in Entity Framework

Source: Internet
Author: User

If you have not touched on or do not know what Entity Framework is, please refer to some basic concepts and operation methods of the Entity Framework here. In a word, Entity Framework is a new data operation Framework of Microsoft. The introduction of Entity Framework in a project greatly facilitates developers to complete various operations on programs and databases. In the early days. in the. NET application, we directly use SqlConnection, SqlDataAdapter, DataReader, and other basic objects to perform operations with the database. Each interaction with the database requires a lot of code. In order to release the programmer's energy from these seemingly boring code and let more people focus on program business operations, various class libraries that interact with the database emerged, some are open-source and some are general class libraries compiled by programmers themselves. The famous one here is nhibw.net, a Mapping-based relational database framework. NHibernate is powerful. However, due to its slightly complicated configuration and lack of the default VS code generation tool, it is often difficult to use it in fast and small projects. The emergence of Entity Framework (hereinafter referred to as EF) fundamentally solves this problem, allowing programmers to completely release the underlying code for interacting with the database, the interaction with the database has become an unprecedented simplicity.

When using EF to interact with relational databases, it is essential to inevitably load data. You can imagine how to use EF to load data in a table containing hundreds of thousands of data records? Load all data to the server memory at one time or load data step by step over and over again in a loop? The specific analysis of the data loading method is required. We cannot decide which method is better or worse here. But one thing that needs to be followed is how to improve the efficiency of data loading. EF provides several different data loading methods, which can be used flexibly according to different needs.

Let's briefly explain how to create an environment. Skip this step if you are familiar with these steps.

1. Create a sample project in Visual Studio. The simplest is eagleapplication.

2. Add ADO. NET Entity Data Model to the project. As shown in,

Select two tables as an example. The relationship between the table Teacher and the table Course is as follows,

(Ctx = q = t (teacher (course

The following are the results of program execution and tracking records in SQL Server Profiler. As you can see clearly, the table Teacher only performs one query. Because the table has only eight records, eight queries of the table Course are generated in the internal loop.

(Ctx = q = t (teacher (} (course

Note that the inner loop obtains data only after the highlighted code is executed. Otherwise, the returned result is 0. By adding judgment conditions, we can control the data loading mode to effectively reduce the number of interactions between programs and databases. In most cases, the data we obtain from the database is not useful. If only a few pieces of data are useful at a time, so why should we filter out useless data to minimize the number of data interactions?

 

Eager Loading)

If you want to load all data to the memory at a time, you need to use the. Include (Entity) method. See the following code,

 ( ctx =  q =  t  ctx.Teachers.Include( ( teacher  ( course 

If you view the tracing information in SQL Server Profiler, you will find that only one data interaction process is performed, that is, the program obtains all the required data only through one query. In the layered structure, this method is the easiest. We can return the results obtained from the underlying database to the upper layer without any dependencies. At the same time, it can reduce the number of interactions between programs and databases. However, there are still some disadvantages, that is, if the data volume is large, loading all the data into the memory at one time is often not the best choice .. The Include (Entity) method allows cascade. You can pre-load data with a multi-level structure.

As mentioned above, the data loading method needs to be selected from time to time. Each data loading method has its own meaning, but there is only one purpose, that is to obtain the required data at the minimum cost.

Related Articles: Loading Related Objects (Entity Framework)

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.