原文:Loading Related Entities

EF加载数据的方式:

  1. 预加载 eager loading
  2. 延迟加载 lazy loading
  3. 显示加载 explicit loading

预先加载会加载所有相关的实体,通过Include方法来实现

 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();
}

延迟加载只有相关的属性被访问时才去加载,延迟加载的的实现是在导航属性前加上virtual

 public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Tags { get; set; } public virtual ICollection<Post> Posts { get; set; }
}

对于包含导航属性的实体,如果要序列化最好关闭延迟加载

对于所有的实体都关闭延迟加载可以在context中设置

 public class BloggingContext : DbContext
{
public BloggingContext()
{
this.Configuration.LazyLoadingEnabled = false;
}
}

尽管延迟加载被禁用,仍然可以使用系显示加载去加载相关的实体

 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();
}
//Reference放法在导航属性对应一个单独的实体时使用
//Collection 方法在导航属性对应一个实体集合时使用

当显示加载相关实体时可以使用filters

 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();
} using (var context = new BloggingContext())
{
var blog = context.Blogs.Find(); // Count how many posts the blog has
var postCount = context.Entry(blog)
.Collection(b => b.Posts)
.Query()
.Count();
}

EF加载实体的方式的更多相关文章

  1. EF中加载实体的方式

    EF中的查询执行时机:1. foreach进行枚举2. ToArray.ToList.ToDictionary3. Linq的一些操作,如First.Any4. DbSet上的Load操作.DbEnt ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (28) ------ 第五章 加载实体和导航属性之测试实体是否加载与显式加载关联实体

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-11  测试实体引用或实体集合是否加载 问题 你想测试关联实体或实体集合是否已经 ...

  3. 《Entity Framework 6 Recipes》中文翻译系列 (22) -----第五章 加载实体和导航属性之延迟加载

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第五章 加载实体和导航属性 实体框架提供了非常棒的建模环境,它允许开发人员可视化地使 ...

  4. 《Entity Framework 6 Recipes》中文翻译系列 (27) ------ 第五章 加载实体和导航属性之关联实体过滤、排序、执行聚合操作

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-9  关联实体过滤和排序 问题 你有一实体的实例,你想加载应用了过滤和排序的相关 ...

  5. 关于实现Extjs动态加载类的方式实现

    Extjs4以前的版本没有动态加载类的方式,这样开发程序的时候加载很多的js会导致加载变慢,由于本人一直使用extjs3的版本进行开发,于是简单实现了一个动态加载类的管理器,使用方式与extjs4的方 ...

  6. Spring之IOC容器加载初始化的方式

    引言 我们知道IOC容器时Spring的核心,可是如果我们要依赖IOC容器对我们的Bean进行管理,那么我们就需要告诉IOC容易他需要管理哪些Bean而且这些Bean有什么要求,这些工作就是通过通过配 ...

  7. Android中加载事件的方式

    Android中加载事件的方式 通过内部类的方式实现 通过外部类的方式实现 通过属性的方式实现 通过自身实现接口的方式实现 通过内部类的方式实现 Demo btn_Login.setOnClickLi ...

  8. UIImage加载图片的方式以及Images.xcassets对于加载方法的影响

    UIImage加载图片的方式以及Images.xcassets对于加载方法的影响 图片缓存 根据是否将创建好的对象缓存入系统内存,有两类创建UIImage对象的方法可选: 缓存:+ imageName ...

  9. UE4中资源加载资源的方式

    在UNITY中,我们加载资源一般是通过Resources.Load(path).即可完成.该方法返回的是Object类型.如果你想要的是材质或者贴图等等,只要价格类型转换的关键字就可以了例如 as M ...

随机推荐

  1. html 使用表单标签,与用户交互

    使用表单标签,与用户交互 网站怎样与用户进行交互?答案是使用HTML表单(form).表单是可以把浏览者输入的数据传送到服务器端,这样服务器端程序就可以处理表单传过来的数据. 语法: <form ...

  2. (转)ASP.NET 伪静态

    1.伪静态:http://www.cnblogs.com/Default.html 目的就是为了赢得更多的收入,至于真否,待SEOer 解答,正如文字所说,伪静态就是假的静态. 2.准备工作:下载Ur ...

  3. html拼接数据的时候一定要注意null值的问题

    后台会返回null文本  如果直接拼接 不仅仅格式问题 前台会显示null   如果是图片  用fiddle抓取 还会发现你请求了一个带域名/null的接口     所以要把null格式化为空文本

  4. javascript使用for循环批量注册的事件不能正确获取索引值的解决方法

    今天遇到一个问题,那就是当使用for循环批量注册事件处理函数,然后最后通过事件处理函数获取当前元素的索引值的时候会失败,先看一段代码实例: <script type="text/jav ...

  5. 8 个实用的 Linux netcat 命令示例

    Netcat 或者叫 nc 是 Linux 下的一个用于调试和检查网络工具包.可用于创建 TCP/IP 连接,最大的用途就是用来处理 TCP/UDP 套接字. 这里我们将通过一些实例来学习 netca ...

  6. js的一个稍微高级点的用法

    通过问题来说明: 1.一个系统中,要创造很多对象,为每个对象分配一个唯一的ID 1: var createObj = (function(){ 2: var i = 0; 3: return func ...

  7. Mysql 新建用户以及授权远程连接操作

    1:以root身份登陆mysql终端 mysql -uroot -pmysql 2:创建wx用户,注意密码要加单引号 mysql> create user wx identified by 'w ...

  8. NSIS如何对一整个目录文件夹(包括子文件夹和其中的文件)压缩

    原来不加/r参数,NSIS编译器就会不认识文件夹啊. File /r [dir] Reference: http://stackoverflow.com/questions/7973242/nsis- ...

  9. Android 最简单的SD卡文件遍历程序

    package com.wenhao.test.sddemo; import java.io.File; import android.app.Activity; import android.os. ...

  10. MySQL常用Json函数

    官方文档:JSON Functions Name Description JSON_APPEND() Append data to JSON document JSON_ARRAY() Create ...