码迷,mamicode.com
首页 > 其他好文 > 详细

EF中加载实体的方式

时间:2015-07-21 23:46:26      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

加载实体的方式:

1.贪婪加载(eager loading)

2.延迟加载(lazy loading)

3.显示加载(explicit loading)

 

贪婪加载实现是通过include方法实现的

技术分享
 1 using (var context = new BloggingContext())
 2 {
 3     // Load all blogs and related posts
 4     var blogs1 = context.Blogs
 5                           .Include(b => b.Posts)
 6                           .ToList();
 7 
 8     // Load one blogs and its related posts
 9     var blog1 = context.Blogs
10                         .Where(b => b.Name == "ADO.NET Blog")
11                         .Include(b => b.Posts)
12                         .FirstOrDefault();
13 
14     // Load all blogs and related posts 
15     // using a string to specify the relationship
16     var blogs2 = context.Blogs
17                           .Include("Posts")
18                           .ToList();
19 
20     // Load one blog and its related posts 
21     // using a string to specify the relationship
22     var blog2 = context.Blogs
23                         .Where(b => b.Name == "ADO.NET Blog")
24                         .Include("Posts")
25                         .FirstOrDefault();
26 }
View Code

延迟加载通过virtual关键字进行实现(当EF框架禁用了延迟加载,这种方式就无效了)

技术分享
 1 public class Blog
 2 { 
 3     public int BlogId { get; set; } 
 4     public string Name { get; set; } 
 5     public string Url { get; set; } 
 6     public string Tags { get; set; } 
 7 
 8     public virtual ICollection<Post> Posts { get; set; } 
 9 }
10 
11 //禁用延迟加载
12 public class BloggingContext : DbContext
13 {
14     public BloggingContext()
15     {
16         this.Configuration.LazyLoadingEnabled = false;
17     }
18 }
View Code

当禁用了延迟加载后或不使用延迟加载时,通过显示加载仍然可以获取管理的数据

技术分享
 1 using (var context = new BloggingContext())
 2 {
 3     var post = context.Posts.Find(2);
 4 
 5     // Load the blog related to a given post
 6     context.Entry(post).Reference(p => p.Blog).Load();
 7 
 8     // Load the blog related to a given post using a string 
 9     context.Entry(post).Reference("Blog").Load();
10 
11     var blog = context.Blogs.Find(1);
12 
13 //一对多时使用Collection
14     // Load the posts related to a given blog
15     context.Entry(blog).Collection(p => p.Posts).Load();
16 
17     // Load the posts related to a given blog 
18     // using a string to specify the relationship
19     context.Entry(blog).Collection("Posts").Load();
20 }
21 
22 //使用Query方式进行一些过滤
23 using (var context = new BloggingContext())
24 {
25     var blog = context.Blogs.Find(1);
26 
27     // Load the posts with the ‘entity-framework‘ tag related to a given blog
28     context.Entry(blog)
29         .Collection(b => b.Posts)
30         .Query()
31         .Where(p => p.Tags.Contains("entity-framework")
32         .Load();
33 
34     // Load the posts with the ‘entity-framework‘ tag related to a given blog 
35     // using a string to specify the relationship 
36     context.Entry(blog)
37         .Collection("Posts")
38         .Query()
39         .Where(p => p.Tags.Contains("entity-framework")
40         .Load();
41 }
View Code

 

EF中加载实体的方式

标签:

原文地址:http://www.cnblogs.com/goodlucklzq/p/4665899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!