Entity Framework Code First -- 延迟加载和预先加载
2013-09-18 12:46
351 查看
还是以这两个表为例子
country包含零个或多个city,这个外键关系是我后来加上去,原来没有。然后再用PowerTool逆向,产生如下代码
[code]usingSystem.Data.Entity.ModelConfiguration;
//Relationships//这里加了一个关系this.HasRequired(t=>t.country)//这个指向city模型的publicvirtualcountrycountry{get;set;}.WithMany(t=>t.cities)//这个指向country模型的publicvirtualICollection<city>cities{get;set;}.HasForeignKey(d=>d.CountryCode);//这个指向city模型的publicstringCountryCode{get;set;}
[/code]
以上是city映射,下面是country映射
country包含零个或多个city,这个外键关系是我后来加上去,原来没有。然后再用PowerTool逆向,产生如下代码
usingSystem.ComponentModel.DataAnnotations.Schema;
[code]usingSystem.Data.Entity.ModelConfiguration;
namespaceEFEntity.Models.Mapping
{
publicclasscityMap:EntityTypeConfiguration<city>
{
publiccityMap()
{
//PrimaryKey
this.HasKey(t=>t.ID);
//Properties
this.Property(t=>t.Name)
.IsRequired()
.HasMaxLength(35);
this.Property(t=>t.CountryCode)
.IsRequired()
.HasMaxLength(3);
this.Property(t=>t.District)
.IsRequired()
.HasMaxLength(20);
//Table&ColumnMappings
this.ToTable("city","world");
this.Property(t=>t.ID).HasColumnName("ID");
this.Property(t=>t.Name).HasColumnName("Name");
this.Property(t=>t.CountryCode).HasColumnName("CountryCode");
this.Property(t=>t.District).HasColumnName("District");
this.Property(t=>t.Population).HasColumnName("Population");
//Relationships//这里加了一个关系this.HasRequired(t=>t.country)//这个指向city模型的publicvirtualcountrycountry{get;set;}.WithMany(t=>t.cities)//这个指向country模型的publicvirtualICollection<city>cities{get;set;}.HasForeignKey(d=>d.CountryCode);//这个指向city模型的publicstringCountryCode{get;set;}
}
}
}
[/code]
以上是city映射,下面是country映射
usingSystem.ComponentModel.DataAnnotations.Schema;
usingSystem.Data.Entity.ModelConfiguration;
namespaceEFEntity.Models.Mapping
{
publicclasscountryMap:EntityTypeConfiguration<country>
{
publiccountryMap()
{
//PrimaryKey
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&ColumnMappings
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(varcontext=newworldContext())
{
varcountry=fromdincontext.countries
whered.Name=="UnitedStates"
selectd;
varcountryUS=country.Single();
if(countryUS==null)return;
foreach(varcityincountryUS.cities)
{
Console.WriteLine(city.Name);
}
Console.Read();
测试代码--预先加载:
using(varcontext=newworldContext())
{
varallCountries=context.countries.Include(p=>p.cities);
foreach(varcountryinallCountries)
{
Console.WriteLine(country.Name);
foreach(varcityincountry.cities)
{
Console.WriteLine("__"+city.Name);
}
}
Console.Read();
}
相关文章推荐
- Entity Framework Code First实体关联数据加载
- Entity Framework Code First实体关联数据加载
- Entity Framework Code First实体关联数据加载
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
- Entity Framework Code First debug
- Entity Framework Code First 迁移数据库
- 【译著】Code First :使用Entity. Framework编程(5)
- Code First :使用Entity. Framework编程(3)
- EntityFramework Codefirst 数据库初始化
- Programming Entity Framework CodeFirst--数据库约定和配置
- EntityFramework Codefirst Select 查询指定列
- Entity Framework Code First使用者的福音 --- EF Power Tool使用记之一
- Entity Framework Code First添加修改及删除外键关联实体
- Entity Framework Code-First(14):From Existing DB
- Entity Framework Code First 学习日记(4)
- Entity Framework Code First映射配置
- EntityFramework Code-First 简易教程(四)-------继承策略
- Entity Framework Code First实现乐观并发
- Entity Framework Code First (五)Fluent API - 配置关系
- Entity Framework CodeFirst For Oracle[转]