博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework Code First -- 延迟加载和预先加载
阅读量:5153 次
发布时间:2019-06-13

本文共 4050 字,大约阅读时间需要 13 分钟。

还是以这两个表为例子

 

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

转载于:https://www.cnblogs.com/grkin/p/3328226.html

你可能感兴趣的文章
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>
Java学习 · 初识 面向对象深入一
查看>>
源代码如何管理
查看>>
vue怎么将一个组件引入另一个组件?
查看>>
bzoj1040: [ZJOI2008]骑士
查看>>
LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
查看>>
利用SignalR来同步更新Winfrom
查看>>
反射机制
查看>>
CocoaPod
查看>>
BZOJ 1251: 序列终结者 [splay]
查看>>
5G边缘网络虚拟化的利器:vCPE和SD-WAN
查看>>
MATLAB基础入门笔记
查看>>
【UVA】434-Matty&#39;s Blocks
查看>>
Android开发技术周报 Issue#80
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
django知识点总结
查看>>
C++ STL stack、queue和vector的使用
查看>>
使用Reporting Services时遇到的小问题
查看>>
约瑟夫问题
查看>>