天天看點

EF的兩種資料加載方式

Eagerly Loading

{ 
    // Load all blogs and related posts 
    var blogs1 = context.Blogs 
                          .Include(b => b.Posts) 
                          .ToList(); 
 
    // Load one blogs and its related posts 
    var blog1 = context.Blogs 
                        .Where(b => b.Name == "ADO.NET Blog") 
                        .Include(b => b.Posts) 
                        .FirstOrDefault(); 
 
    // Load all blogs and related posts  
    // using a string to specify the relationship 
    var blogs2 = context.Blogs 
                          .Include("Posts") 
                          .ToList(); 
 
    // Load one blog and its related posts  
    // using a string to specify the relationship 
    var blog2 = context.Blogs 
                        .Where(b => b.Name == "ADO.NET Blog") 
                        .Include("Posts") 
                        .FirstOrDefault(); 
}
           
using (var context = new BloggingContext()) 
{ 
    // Load all blogs, all related posts, and all related comments 
    var blogs1 = context.Blogs 
                       .Include(b => b.Posts.Select(p => p.Comments)) 
                       .ToList(); 
 
    // Load all users their related profiles, and related avatar 
    var users1 = context.Users 
                        .Include(u => u.Profile.Avatar) 
                        .ToList(); 
 
    // Load all blogs, all related posts, and all related comments  
    // using a string to specify the relationships 
    var blogs2 = context.Blogs 
                       .Include("Posts.Comments") 
                       .ToList(); 
 
    // Load all users their related profiles, and related avatar  
    // using a string to specify the relationships 
    var users2 = context.Users 
                        .Include("Profile.Avatar") 
                        .ToList(); 
}               

collection指向對象集而reference指向的是單個對象

using (var context = new BloggingContext()) 
{ 
    var post = context.Posts.Find(); 
 
    // Load the blog related to a given post 
    context.Entry(post).Reference(p => p.Blog).Load(); 
 
    // Load the blog related to a given post using a string  
    context.Entry(post).Reference("Blog").Load(); 
 
    var blog = context.Blogs.Find(); 
 
    // Load the posts related to a given blog 
    context.Entry(blog).Collection(p => p.Posts).Load(); 
 
    // Load the posts related to a given blog  
    // using a string to specify the relationship 
    context.Entry(blog).Collection("Posts").Load(); 
}                      The Query method provides access to the underlying query that the Entity Framework will use when loading related entities. You can then use LINQ to apply              filters to the query before executing it with a call to a LINQ extension method such as ToList, Load, etc. The Query method can be used with both reference              and collection navigation properties but is most useful for collections where it can be used to load only part of the collection.
                       
using (var context = new BloggingContext()) 
{ 
    var blog = context.Blogs.Find(); 
 
    // Load the posts with the 'entity-framework' tag related to a given blog 
    context.Entry(blog) 
        .Collection(b => b.Posts) 
        .Query() 
        .Where(p => p.Tags.Contains("entity-framework") 
        .Load(); 
 
    // Load the posts with the 'entity-framework' tag related to a given blog  
    // using a string to specify the relationship  
    context.Entry(blog) 
        .Collection("Posts") 
        .Query() 
        .Where(p => p.Tags.Contains("entity-framework") 
        .Load(); //or executed by ToList() method
}
           

繼續閱讀