Entity Framework provides three methods for loading related entities: lazy loading, eager loading, and explicit loading. First, let's take a look at msdn's definition of three methods for loading objects.
Lazy Loading: For this type of loading, related entities are automatically loaded from the data source when you access the navigation attributes. When using this loading type, note that if the object is not in objectcontext, each navigation attribute you access will cause a separate query for the data source.
Eager Loading: When you understand the applicationProgramYou can use the include method of objectquery to define the query path for the exact shape of the graph of the expected object. This query path controls which objects are returned as part of the initial query. When defining a query path, you only need to request the database once to return all objects defined in the query path in a single result set, all related entities of the types defined in the path will be loaded along with each object returned by the query.
Explicit Loading: To explicitly load an object to objectcontext requires multiple round-trips to the database and multiple active result sets. However, the returned data volume is limited to the loaded object. You can use the load method for entitycollection or entityreference or the loadproperty Method for objectcontext to explicitly retrieve relevant entities from the data source. Each call to the load method opens a connection to the database to retrieve relevant information. This ensures that the query is never executed when no explicit request is made to the relevant entity.
Next we will test the above three loading methods one by one.
Before testing, we should first create a database for testing and insert some data into it:
Figure 1
Lazy Loading
Lazyloading is enabled by default in Entity framework4.0 and later versions. After the model is generated from the database, you can click in the blank area of the edmx file and see this setting in the Properties window:
Figure 2
You can also open the edmx file in XML format and see this setting in the CSDL section:
View code
1 <! -- CSDL content -->
2 <edmx: conceptualmodels>
3 <schema namespace = "testmodel" alias = "self" xmlns: annotation = "http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns = "http://schemas.microsoft.com/ado/2008/09/edm">
4 <entitycontainer name = "testentities" annotation: lazyloadingenabled = "true">
5 <entityset name = "players" entitytype = "testmodel. Player"/>
6 <entityset name = "playerdetails" entitytype = "testmodel. playerdetail"/>
7 <entityset name = "teams" entitytype = "testmodel. Team"/>
8 <associationset name = "fk_player_team1" association = "testmodel. fk_player_team1">
9 <End role = "team" entityset = "teams"/>
10 <End role = "Player" entityset = "players"/>
11 </associationset>
12 <associationset name = "fk_playerdetails_player" association = "testmodel. fk_playerdetails_player">
13 <End role = "Player" entityset = "players"/>
14 <End role = "playerdetails" entityset = "playerdetails"/>
15 </associationset>
16 </entitycontainer>
Note:: Lazy loading is set for all models, not a specific model.
Next, let's write a simpleCodeTo test the lazy loading:
View code
Using (VAR context = new testentities ())
{
Iqueryable <team> teams = from T in context. Teams select T;
Foreach (team T in teams)
{
Console. writeline (T. Players. Count ());
}
Console. Read ();
}
The running result is as follows:
Figure 3
We can see that in the query statement, we only need to return all team information, and there is no information like the player to be loaded in the database request, but in the foreach statement, we want to print out the number of players for each team, but it is successful. This is the effect of lazy loading. In fact, when the count statement is executed, the program will fetch the request database and return the player information. That is to say, if we have 100 teams, the program will access the database for 100 times to perform this operation.
Next we will disable lazy loading to see the effect. There are multiple ways to disable lazy loading. We can set lazy loading enabled to false in the attribute window in Figure 2, or assign the value of lazy loading enabled to false in XML code, here we use the program code to close lazy loading and execute the above Code to see the effect:
View code
Using (VAR context = new testentities ())
{
// Disable lazy loading
Context. contextoptions. lazyloadingenabled = false;
Iqueryable <team> teams = from T in context. Teams select T;
Foreach (team T in teams)
{
Console. writeline (T. Players. Count ());
}
Console. Read ();
}
The execution result is as follows:
Figure 4
From the execution results, we can see that when the foreach statement is executed, the program does not query the database, and our query statement does not request information about the player from the database, therefore, the number of players cannot be printed.
Finally, let's summarize the advantages and disadvantages of lazy loading: When lazy loading is enabled, we don't have to worry about whether an object has been loaded, it does not occur when a certain entity is called. It saves the programmer a lot of effort, but the disadvantage is also very obvious. If we have a large number of entities and frequently call related entities, the program will frequently access the database, which obviously affects the program performance.