MVC-EF delayed loading and mvcef delayed Loading

Source: Internet
Author: User

MVC-EF delayed loading and mvcef delayed Loading

The so-called EF delayed loading means that when you use Lamabda or Linq to query data, EF does not directly query the data, but loads the data into the memory only when the query result is used. Delayed loading can also be understood as loading on demand. As the name suggests, loading data according to the required data. So why do we need to use delayed loading? What are the advantages and disadvantages of using delayed loading? The following are examples.

First, we used two very simple tables.

T_Artcle Article table, T_User table, using UserID as the foreign key Association, insert the following data

Insert into T_User VALUES ('nee32 ')
Insert into T_Artcle VALUES ('test title', 1)

Create a new console application and import the two tables. Note that when adding the table, you can check the singular and plural forms of the names of the generated objects.

I. Difference between Query Where in EF and query Where in set List

Where in EF

Where in EF returns an IQueryable. Let's write another list set to see what is returned.

Strange, they are all the Where methods, but the returned results are completely different becauseThe query method of the set Where is from System. linq. the extension method added to the IEnumerable interface in Enumerable, while the query method in DBSet <T> in EF context Where comes from System. linq. extension Method added to the IQueryable interface in QueryableThe Where method is called, but the two methods are completely different. Because of the IQueryable interface, latency query is supported.

Ii. Instant query and delayed Query

1. Instant Query

Create a breakpoint when executing the query.

Use SQL server profiler to listen for the executed SQL

You can see that the database is queried immediately after the breakpoint is executed, so the above query is real-time

2. Delayed Query

Let's change the code above.

Set the breakpoint and use SQL server profiler to listen again. No SQL is executed before the breakpoint is found.

T_User user = query. FirstOrDefault ()

Advantages of delayed query: for example, there is a blog query page with query conditions such as release time, keyword, and category. When the number of query conditions is unknown, where does not query the database immediately, instead, after all the conditions are determined, a corresponding SQL statement is generated based on these where conditions to query the database.

3. Delayed loading of foreign key entities

Essence: For foreign key attributes, EF queries the corresponding table only when this foreign key attribute is used.

First, let's look at the relationship between users and articles in the EDM entity model.

The generated class code is as follows:

 

 public partial class T_Artcle    {        public int Id { get; set; }        public string Title { get; set; }        public int UserID { get; set; }            public virtual T_User T_User { get; set; }    }
public partial class T_User    {        public T_User()        {            this.T_Artcle = new HashSet<T_Artcle>();        }            public int Id { get; set; }        public string UserName { get; set; }            public virtual ICollection<T_Artcle> T_Artcle { get; set; }    }

Edmx helps us generate the corresponding primary/foreign key relationship.

For example, you can query the article users whose table ID is 1.

Static void Main (string [] args) {Nee32Entities db = new Nee32Entities (); // create a context object var query = db. t_Artcle.Where (u => u. id = 1); T_Artcle artcle = query. firstOrDefault (); // only query the Document Table Console. writeLine (artcle. t_User.UserName); // query the user table Console corresponding to the article. readKey ();}

Set a breakpoint in Console. WriteLine (artcle. T_User.UserName); and use SQL server profiler to listen.

Only the article table is queried here, and the user table is not queried. Continue step by step.

At this time, EF uses the user table, because the foreign key attribute of the article table is used here, and the program outputs nee32

End

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.