Entity Framework Code First -- 延迟加载和预先加载
还是以这两个表为例子

country包含零个或多个city, 这个外键关系是我后来加上去,原来没有。 然后再用Power Tool逆向, 产生如下代码
1: using System.ComponentModel.DataAnnotations.Schema;
2: using System.Data.Entity.ModelConfiguration;
3:
4: namespace EFEntity.Models.Mapping
5: {
6: public class cityMap : EntityTypeConfiguration<city>
7: {
8: public cityMap()
9: {
10: // Primary Key
11: this.HasKey(t => t.ID);
12:
13: // Properties
14: this.Property(t => t.Name)
15: .IsRequired()
16: .HasMaxLength(35);
17:
18: this.Property(t => t.CountryCode)
19: .IsRequired()
20: .HasMaxLength(3);
21:
22: this.Property(t => t.District)
23: .IsRequired()
24: .HasMaxLength(20);
25:
26: // Table & Column Mappings
27: this.ToTable("city", "world");
28: this.Property(t => t.ID).HasColumnName("ID");
29: this.Property(t => t.Name).HasColumnName("Name");
30: this.Property(t => t.CountryCode).HasColumnName("CountryCode");
31: this.Property(t => t.District).HasColumnName("District");
32: this.Property(t => t.Population).HasColumnName("Population");
33:
34: // Relationships //这里加了一个关系 35: this.HasRequired(t => t.country) //这个指向city 模型的 public virtual country country { get; set; } 36: .WithMany(t => t.cities) //这个指向country模型的 public virtual ICollection<city> cities { get; set; } 37: .HasForeignKey(d => d.CountryCode); //这个指向city模型的public string CountryCode { get; set; }
38:
39: }
40: }
41: }
以上是city映射, 下面是country映射
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
namespace EFEntity.Models.Mapping
{
public class countryMap : EntityTypeConfiguration<country>
{
public countryMap()
{
// Primary Key
this.HasKey(t => t.Code);
// Properties
this.Property(t => t.Code)
.IsRequired()
.HasMaxLength(3);
this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(52);
this.Property(t => t.Continent)
.IsRequired()
.HasMaxLength(65532);
this.Property(t => t.Region)
.IsRequired()
.HasMaxLength(26);
this.Property(t => t.LocalName)
.IsRequired()
.HasMaxLength(45);
this.Property(t => t.GovernmentForm)
.IsRequired()
.HasMaxLength(45);
this.Property(t => t.HeadOfState)
.HasMaxLength(60);
this.Property(t => t.Code2)
.IsRequired()
.HasMaxLength(2);
// Table & Column Mappings
this.ToTable("country", "world");
this.Property(t => t.Code).HasColumnName("Code");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.Continent).HasColumnName("Continent");
this.Property(t => t.Region).HasColumnName("Region");
this.Property(t => t.SurfaceArea).HasColumnName("SurfaceArea");
this.Property(t => t.IndepYear).HasColumnName("IndepYear");
this.Property(t => t.Population).HasColumnName("Population");
this.Property(t => t.LifeExpectancy).HasColumnName("LifeExpectancy");
this.Property(t => t.GNP).HasColumnName("GNP");
this.Property(t => t.GNPOld).HasColumnName("GNPOld");
this.Property(t => t.LocalName).HasColumnName("LocalName");
this.Property(t => t.GovernmentForm).HasColumnName("GovernmentForm");
this.Property(t => t.HeadOfState).HasColumnName("HeadOfState");
this.Property(t => t.Capital).HasColumnName("Capital");
this.Property(t => t.Code2).HasColumnName("Code2");
}
}
}
测试代码--延迟加载:
using (var context = new worldContext())
{
var country = from d in context.countries
where d.Name == "United States"
select d;
var countryUS = country.Single();
if (countryUS == null) return;
foreach (var city in countryUS.cities)
{
Console.WriteLine(city.Name);
}
Console.Read();
测试代码--预先加载:
using (var context = new worldContext())
{
var allCountries = context.countries.Include(p => p.cities);
foreach (var country in allCountries)
{
Console.WriteLine(country.Name);
foreach (var city in country.cities)
{
Console.WriteLine("__" + city.Name);
}
}
Console.Read();
}
Entity Framework Code First -- 延迟加载和预先加载的更多相关文章
- Entity Framework入门教程(8)---预先加载、延迟加载、显示加载
1.预先加载 预先加载:在对一种类型的实体进行查询时,将相关的实体作为查询的一部分一起加载.预先加载可以使用Include()方法实现. 1.加载一个相关实体类型 栗子:使用Include()方法从数 ...
- Entity Framework Code First实体关联数据加载
在项目过程中,两个实体数据之间在往往并非完全独立的,而是存在一定的关联关系,如一对一.一对多及多对多等关联.存在关联关系的实体,经常根据一个实体的实例来查询获取与之关联的另外实体的实例. Entity ...
- EF 延迟加载和预先加载
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本节探讨延迟加载和预先加载 Entity Frame ...
- Entity Framework关联实体的三种加载方法
推荐文章 EF性能之关联加载 总结很好 一:介绍三种加载方式 Entity Framework作为一个优秀的ORM框架,它使得操作数据库就像操作内存中的数据一样,但是这种抽象是有性能代价的,故鱼和熊掌 ...
- Entity Framework Code First学习系列目录
Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...
- Entity Framework Code First学习系列
Entity Framework Code First学习系列目录 Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity ...
- Entity Framework Code First关系映射约定
本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...
- Entity Framework Code First主外键关系映射约定
本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...
- Entity Framework Code First关系映射约定【l转发】
本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...
随机推荐
- Linux - redis发布|订阅
目录 Linux - redis发布|订阅 发布|订阅 基本命令 发布和订阅实例 正则方式订阅一个或者多个符合模式的频道 Linux - redis发布|订阅 发布: publish 订阅: subs ...
- SQLServer中的Cross Apply、Outer Apply
https://www.2cto.com/database/201304/206330.html
- GlobalSign 企业型SSL 证书
GlobalSign 企业型SSL 证书 GlobalSign 企业型 SSL 证书属于OV SSL,进行严格的网站所有权的验证,企业真实身份验证,证书标识企业组织机构名称,增加信任度.提供40位 ...
- HDU - 1403 - Longest Common Substring
先上题目: Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- nyoj_19_擅长排列的小明_201403011600
擅长排列的小明时间限制:1000 ms | 内存限制:65535 KB 难度:4描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他, ...
- [bzoj1468][poj1741]Tree_点分治
Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...
- P1294 高手去散步 洛谷
https://www.luogu.org/problem/show?pid=1294#sub 题目背景 高手最近谈恋爱了.不过是单相思.“即使是单相思,也是完整的爱情”,高手从未放弃对它的追求.今天 ...
- [转]十五天精通WCF——第三天 client如何知道server提供的功能清单
通常我们去大保健的时候,都会找姑娘问一下这里能提供什么服务,什么价格,这时候可能姑娘会跟你口述一些服务或者提供一份服务清单,这样的话大 家就可以做到童嫂无欺,这样一份活生生的例子,在wcf中同样是一 ...
- Git 主要的工作流程
Git使用个进制字符的SHA- Hash来唯一标识对象 如:e98757d0598ab6eeaf1df0d87dd00826048bd80b git 有种对象 1.blob 表示文本文件,二进制文件或 ...
- Swift基础(类,结构体,函数)
import Foundation // 创建一个类 class Student { // 属性(类的属性必须赋初值,如果不赋值,需要写自定义方法) var studentName: String v ...