EF中加载实体的方式

 EF中的查询执行时机:
1. foreach进行枚举
2. ToArray、ToList、ToDictionary
3. Linq的一些操作,如First、Any
4. DbSet上的Load操作。DbEntityEntry.Reload和Database.ExecuteSqlCommand

在web application中,每一个请求使用一个context实例;在WPF中,每个form使用一个context实例
context不是线程安全的

加载实体的方式:

1.贪婪加载(eager loading)

2.延迟加载(lazy loading)

3.显示加载(explicit loading)

 

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

EF中加载实体的方式
 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框架禁用了延迟加载,这种方式就无效了)

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

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

EF中加载实体的方式
 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

 

你可能感兴趣的:(加载)