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方法实现的
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();
}
延迟加载通过virtual关键字进行实现(当EF框架禁用了延迟加载,这种方式就无效了)
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; }
} //禁用延迟加载
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(); //一对多时使用Collection
// 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();
} //使用Query方式进行一些过滤
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();
}
EF中加载实体的方式的更多相关文章
- Android中加载事件的方式
Android中加载事件的方式 通过内部类的方式实现 通过外部类的方式实现 通过属性的方式实现 通过自身实现接口的方式实现 通过内部类的方式实现 Demo btn_Login.setOnClickLi ...
- EF加载实体的方式
原文:Loading Related Entities EF加载数据的方式: 预加载 eager loading 延迟加载 lazy loading 显示加载 explicit loading 预先加 ...
- Spring中加载配置文件的方式
原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...
- UE4中资源加载资源的方式
在UNITY中,我们加载资源一般是通过Resources.Load(path).即可完成.该方法返回的是Object类型.如果你想要的是材质或者贴图等等,只要价格类型转换的关键字就可以了例如 as M ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- Spring中加载ApplicationContext.xml文件的方式
Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...
随机推荐
- UML基础知识
UML:Unified Modeling Language,即统一建模语言.是一种图形化的建模语言标准. 如上图,UML可以帮助我们做软件需求分析和软件设计两方面的工作,在不同的应用场景中,UML的一 ...
- Android-操作栏之副标题
我们的目标是在操作栏右侧加上一个选项菜单,点击它就可显示或者隐藏操作栏的副标题. 由于操作栏是在API11级以后出现的,因此必须考虑兼容性问题.我们直接让低于API11的设备根本看不到选项菜单即可.建 ...
- Intent 能传递的数据类型
1. Serializable,将对象序列化成二进制数据传递 2. charsequence: 主要用来传递String,char等 3. parcelable: 这个android提供的一种新的类型 ...
- Retinex processing for automatic image enhancement 翻译
Retinex processing for automatic image enhancement 摘要: 最近六七年来,人们从新燃起了对Retinex computation的兴趣,特别是在它对图 ...
- 转。webapp开发小tips
备忘 - Q: webapp点击一个按钮调用系统拨号: <a href="tel:12345654321">打电话给我</a> <a href=& ...
- struts2标签自动错行、换行问题
<s:radio list="#{0:'男',1:'女' }" value="student.sex" name="student.sex&qu ...
- jQuery Lazy Load 图片延迟加载
基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...
- Python 基础教程中的问题及解决方案(1)
1. 在ubuntu中,调用终端时如: f = open('/home/theone/test_input.txt', 'r') 中的txt格式文本不能加后缀 正确的应为: f = open('/h ...
- linux内核学习之三:linux中的"32位"与"64位"
在通用PC领域,不论是windows还是linux界,我们都会经常听到"32位"与"64位"的说法,类似的还有"x86"与"x86 ...
- FastMM内存泄露
转自:http://www.2ccc.com/article.asp?articleid=4879FastMM是非常优秀的内存管理器,但是从FastMM4Options.inc中找到合适自已程序的选项 ...