Has方法与With方法

A.HasRequired(a => a.B).WithOptional(b => b.A);
上面一句配置意思就是A类包含B类一个不为null的实例,B类包含A类一个实例,也可以不包含。

Has方法:

  1. HasOptional:前者(A)包含后者(B)一个实例或者为null
  2. HasRequired:前者包含后者一个不为null的实例
  3. HasMany:前者包含后者实例的集合

With方法:

  1. WithOptional:后者(B)可以包含前者(A)一个实例或者null
  2. WithRequired:后者包含前者一个不为null的实例
  3. WithMany:后者包含前者实例的集合

一、一对一关系:

两个类中都要配置相应的引用属性

1、DataAnnotations方式

public class Person
{
public int PersonId { get; set; }
public int SocialSecurityNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public PersonPhoto Photo { get; set; }
} public class PersonPhoto
{
[Key, ForeignKey("PhotoOf")] 注意:PersonPhoto表中的PersonId既是外键也必须是主键
public int PersonId { get; set; }
public byte[] Photo { get; set; }
public string Caption { get; set; }
public Person PhotoOf { get; set; }
}

2、Fluent API方式

1:0..1关系

modelBuilder.Entity<Person>().HasOptional(t => t.Photo).WithRequired(t => t.PhotoOf); 
modelBuilder.Entity<PersonPhoto>().HasRequired(p => p.PhotoOf).WithOptional(p => p.Photo);

PersonPhoto必须属于一个Person,但是Person不一定有PersonPhoto, 此种情况下Person是一定存在的,所以它是主从关系主的一方。

1:1 关系

modelBuilder.Entity<Person>().HasRequired(p => p.Photo ).WithRequiredPrincipal();
modelBuilder.Entity<PersonPhoto>().HasRequired(t => t.PhotoOf).WithRequiredDependent(t => t.Photo);

PersonPhoto必须属于一个Person,Person也必须有PersonPhoto。
此种情况下,两个都一定存在,要确定主从关系,需要使用WithRequiredPrincipal或WithRequiredDependent。

如果你选择WithOptionalPrincipal(当前实体为主体;目标实体为依赖对象,包含主体的外键)PersonPhoto表中有一个外键,指向Person表的主键。
如果你选择WithOptionalDependent则相反(当前实体为依赖对象,包含主体的外键;目标实体为主体)则代表Person表中有一个外键,指向PersonPhoto表的主键,

Person表可以没有对应的PersonPhoto表数据,但是PersonPhoto表每一条数据都必须对应一条Person表数据。意思就是人可以没有照片,但是有的照片必须属于某个人。

二、一对多关系:

1、DataAnnotations方式

一对多关系很多情况下我们都不需要特意的去配置,通过一些引用属性、导航属性等检测到模型之间的关系,自动为我们生成外键。

public class Destination
{//景点类
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
} public class Lodging
{//住宿类
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
public Destination Target { get; set; }
}

Code First观察到Lodging类中有一个对Destination的引用属性,或者Destination中又有一个集合导航属性Lodgings,
因此推测出Destination与Lodging的关系是一对多关系,所以在生成的数据库中为自动为Lodging表生成外键:Target_DestinationId

2、更改外键的nullable属性和外键的名字

默认情况下,如果你的外键命名是规范的话,Code First会将的该属性设置为外键,不再自动创建一个外键,
规范命名是指符合:命名为如下的形式

  • [目标类型的键名]
  • [目标类型名称]+[目标类型键名称]
  • [导航属性名称]+[目标类型键名称]

在这里目标类型就是Destination,相对应的命名就是:DestinationId,DestinationDestinationId,TargetDestinationId

当然我们也可以自己在类中增加一个外键。

使用Data Annotations指定外键:注意ForeignKey位置的不同,其后带的参数也不同。

[ForeignKey("Target")]
public int TarDestinationId { get; set; }
public Destination Target { get; set; }

public int TarDestinationId { get; set; }
[ForeignKey("TarDestinationId")]
public Destination Target { get; set;

用Fluent API指定外键:

1.Lodging一定归属于一个Destination,这种关系是1:n。

modelBuilder.Entity<Destination>().HasMany(d => d.Lodgings).WithRequired(l => l.Destination).Map(l => l.MapKey("DestinationId"));
modelBuilder.Entity<Lodging>().HasRequired(l => l.Target).WithMany(d=>d.Lodgings).HasForeignKey(l => l.TarDestinationId);
//如果实体类没定义AccommodationId,那么可以使用Map方法直接指定外键名:.Map(s => s.MapKey("AccommodationId"))

2.Post可以单独存在,不用归属于Blog,这种关系是0..1:n。

modelBuilder.Entity<Destination>().HasMany(d => d.Lodgings).WithOptional(l => l.Destination).Map(l => l.MapKey("DestinationId")); modelBuilder.Entity<Lodging>().HasOptional(l => l.Target).WithMany(d => d.Lodgings).HasForeignKey(l => l.TarDestinationId);

3、对同一实体多个引用的情况

public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Lodging> PrimaryContactFor { get; set; }
public List<Lodging> SecondaryContactFor { get; set; }
} public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
public Destination Target { get; set; }
//第一联系人
public Person PrimaryContact { get; set; }
//第二联系人
public Person SecondaryContact { get; set; }
}

Lodging(旅店)有两个对Person表的引用,分别是PrimaryContact与SecondaryContact,
同时,在Person表中也有对这两个联系人的导航:PrimaryContactFor与SecondaryContactFor。

因为在这两个表之间存在多个一对多关系,所以Code First无法处理这种情况。
为了让Code First知道它们之间的对应关系,在这里要用到逆导航属性来解决。

使用Data Annotations:

//第一联系人
[InverseProperty("PrimaryContactFor")]
public Person PrimaryContact { get; set; }
//第二联系人
[InverseProperty("SecondaryContactFor")]
public Person SecondaryContact { get; set; }

或使用Fluent API:

modelBuilder.Entity<Lodging>().HasOptional(l => l.PrimaryContact).WithMany(p => p.PrimaryContactFor);
modelBuilder.Entity<Lodging>().HasOptional(l=>l.SecondaryContact).WithMany(p=>p.SecondaryContactFor)).Map(p => p.MapKey("SecondaryPersonID "));;

在生成的数据库中为自动为Lodging表生成两个外键:PrimaryContact _PersonID 和SecondaryPersonID

4、级联删除

1、如果两个表之间存在一对多关系,Code First默认会开启两个表之间的级联删除功能
数据库里可以可视化的设置不级联删除,Fluent API配置此外键关系时可以设置不级联删除:

this.HasMany(d => d.Lodgings).WithRequired(l => l.Destination).Map(l => l.MapKey("DestinationId")) //一对多并指定外键名
.WillCascadeOnDelete(false); // 关闭级联删除

2、也可以在上下文的OnModelCreating方法中

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

移除这个默认约定,再在需要开启级联删除的FluentAPI关系映射中用. WillCascadeOnDelete(true) 单独开启

三、多对多关系

如果有两个类中,各自都是导航属性指向另一个类,Code First会认为这两个类之间是多对多关系,例如:

public class Activity
{
public int ActivityId { get; set; }
[Required, MaxLength()]
public string Name { get; set; }
public List<Trip> Trips { get; set; }
} public class Trip
{
public int TripId{get;set;}
public DateTime StartDate{get;set;}
public DateTime EndDate { get; set; }
public decimal CostUSD { get; set; }
public byte[] RowVersion { get; set; }
public List<Activity> Activities { get; set; }
}

Code First生成了一张中间表ActivityTrips,将另外两张表的主键都作为外键关联到了中间表上面。
中间表中键的命名默认为"[目标类型名称]_[目标类型键名称]".Activity_ActivityId 和Trip_TripId
并且也作为这个新的连接表的联合主键。

指定表名

如果我们想指定中间表的名称和键名称,我们可以用Fluent API来配置。

modelBuilder.Entity<Trip>().HasMany(t => t.Activities).WithMany(a => a.Trips).Map(m =>
{
m.ToTable("TripActivities");
m.MapLeftKey("TripIdentifier");//对应Trip的主键
m.MapRightKey("ActivityId");
});

或:

modelBuilder.Entity<Activity>().HasMany(a => a.Trips).WithMany(t => t.Activities).Map(m =>
{
m.ToTable("TripActivities");
m.MapLeftKey("ActivityId");//对应Activity的主键
m.MapRightKey("TripIdentifier");
});

最新文章

  1. 求一个区间[a,b]中数字1出现的次数
  2. asp.net 短信群发
  3. MicrosoftProjectOxford 微软牛津计划
  4. IPointCollection转IPolyline
  5. [Swustoj 24] Max Area
  6. 一起啃PRML - 1.2.3 Bayesian probabilities 贝叶斯概率
  7. hdu 4091 Zombie’s Treasure Chest(数学规律+枚举)
  8. php连接postgresql
  9. Jpa规范中persistence.xml 配置文件解析
  10. 201215-03-19---cocos2dx内存管理--具体解释
  11. 深入理解Java虚拟机 自己编译JDK
  12. dojo省份地市级联之地市封装类(二)
  13. 渗透测试环境DVWA搭建
  14. Nginx的负载均衡 - 加权轮询 (Weighted Round Robin) 上篇
  15. 将文件转成clob添加到Oracle数据库中
  16. Groovy学习笔记-布尔求值
  17. python requests库网页爬取小实例:亚马逊商品页面的爬取
  18. Codeforces1076F. Summer Practice Report(贪心+动态规划)
  19. 【 D3.js 入门系列 --- 2 】 怎样使用数据和选择元素
  20. MySQL数据库函数

热门文章

  1. 编写规范的javascript
  2. WEB前端笔记
  3. win7(64)+vs2010+opencv2.3.1配置问题:应用程序无法正常启动0xc000007b
  4. bzoj 5252: [2018多省省队联测]林克卡特树
  5. CentOS7 一键安装KMS服务【整理】
  6. mysql字符集的修改
  7. Incorrect column count: expected 1, actual 5
  8. jquery点击导航栏选中更换样式
  9. 关于div设置display: inline-block之后盒子之间间距的处理
  10. sftp java 上传