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. ...
随机推荐
- uva 10260 - Soundex
题目:编码翻译,有些字母有对应的数字,有的没有,如果连续对应的数字相同只输出一个. #include <iostream> #include <cstdlib> #includ ...
- 手机网页中的hover效果实现
js文件 function mouseout(obj) { var className ="hover"; var _ecname = obj.className; if (_ec ...
- 3月26日html(七)window document
---恢复内容开始--- 1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: v ...
- html5标签placeholder使用
<!DOCTYPE HTML> <html> <body> <form action="/example/html5/demo_form.asp&q ...
- C语言初学 求100到200的全部素数
#include<stdio.h> #include<math.h> int main() { int m,i,k; for(m=101;m<=200;m=m+2) { ...
- Scala学习文档-访问修饰符
在scala里,对保护成员的访问比Java严格.Scala中,保护成员只在定义了成员的类的子类中可以访问,而Java中,还允许在同一个包的其他类中访问. package p1 { class FCla ...
- FJ省队集训DAY5 T1
思路:考试的时候打了LCT,自以为能过,没想到只能过80.. 考完一想:lct的做法点数是100W,就算是nlogn也会T. 讲一下lct的做法把:首先如果一条边连接的两个点都在同一个联通块内,那么这 ...
- b/s客户端和服务器的交互(转)
原文:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ 作为一个软件开发者,你一定会对网络应用如何工作有 ...
- Linux企业级项目实践之网络爬虫(21)——扩展为多任务爬虫
高效的网络爬虫是搜索引擎的重要基础.采用多任务并发执行,实现类似于CPU的流水线(pipeline)运行方式,可极大地提高网络和计算资源的利用率等性能. #include "threads. ...
- spring framework 4 源码阅读(1) --- 前期准备
在开始看代码之前,需要做的第一件事是下载代码. 在这里:https://github.com/spring-projects/spring-framework 下载完成了发现使用gradle做的源代码 ...