EF的两种数据加载方式

Eagerly Loading

using (var context = new BloggingContext()) 

{ 
    // 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(); 
}
Explicitly Loading

collection指向对象集而reference指向的是单个对象

using (var context = new BloggingContext()) 
{ 
    var post = context.Posts.Find(2); 
 
    // 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(1); 
 
    // 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(1); 
 
    // 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
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值