public class Student
{
public Student() { } public int StudentId { get; set; }
public string StudentName { get; set; } public virtual Standard Standard { get; set; }
} public class Standard
{
public Standard()
{
Students = new List<Student>();
}
public int StandardId { get; set; }
public string Description { get; set; } public virtual ICollection<Student> Students { get; set; }
}

上面的代码中,Student实体包含导航属性Standard,Standard实体包含集合导航属性Student,Code First的默认规则为1对多的关系

指定外键

public class Student
{
public Student() { } public int StudentId { get; set; }
public string StudentName { get; set; } public int StdandardRefId { get; set; } [ForeignKey("StandardRefId")]
public virtual Standard Standard { get; set; }
} public class Standard
{
public Standard()
{
Students = new List<Student>();
}
public int StandardId { get; set; }
public string Description { get; set; } public virtual ICollection<Student> Students { get; set; }
}

Fluent API配置

public class Student
{
public Student(){ } public int StudentId { get; set; }
public string StudentName { get; set; } public int StandardId { get; set; } public virtual Standard Standard { get; set; }
} public class Standard
{
public Standard()
{
StudentsList = new List<Student>();
}
public int StandardId { get; set; }
public string Description { get; set; } public virtual ICollection<Student> Students { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//one-to-many
modelBuilder.Entity<Student>()
.HasRequired<Standard>(s => s.Standard) // Student entity requires Standard
.WithMany(s => s.Students); // Standard entity includes many Students entities }

如果外键不符合默认规则

public class Student
{
public Student(){ } public int StudentId { get; set; }
public string StudentName { get; set; } //StdId is not following code first conventions name
public int StdId { get; set; } public virtual Standard Standard { get; set; }
} public class Standard
{
public Standard()
{
StudentsList = new List<Student>();
}
public int StandardId { get; set; }
public string Description { get; set; } public virtual ICollection<Student> Students { get; set; }
}

可以如下配置

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//one-to-many
modelBuilder.Entity<Student>()
.HasRequired<Standard>(s => s.Standard)
.WithMany(s => s.Students)
.HasForeignKey(s => s.StdId); }

modelBuilder.Entity<Student>().HasRequired<Standard>(s => s.Standard)

表明Student必须包含Standard导航属性,

.WithMany(s => s.Students).HasForeignKey(s => s.StdId)表明Standard有多个Student,外键名为StdId。

另一种方法

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//configure one-to-many
modelBuilder.Entity<Standard>()
.HasMany<Student>(s => s.Students) Standard has many Students
.WithRequired(s => s.Standard) Student require one Standard
.HasForeignKey(s => s.StdId);Student includes specified foreignkey property name for Standard
}

外键可空的配置

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//one-to-many
modelBuilder.Entity<Student>()
.HasOptional<Standard>(s => s.Standard)
.WithMany(s => s.Students)
.HasForeignKey(s => s.StdId); }

配置如发生如下异常

One or more validation errors were detected during model generation:

Domain.Student_Standard: : Multiplicity conflicts with the referential constraint in Role 'Student_Standard_Target' in relationship 'Student_Standard'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

则需要配置外键为nullable类型

EF Code-First 学习之旅 一对多的关系的更多相关文章

  1. EF Code First学习笔记

    EF Code First学习笔记 初识Code First EF Code First 学习笔记:约定配置 Entity Framework 复杂类型 Entity Framework 数据生成选项 ...

  2. EF Code First学习系列

    EF Model First在实际工作中基本用不到,前段时间学了一下,大概的了解一下.现在开始学习Code First这种方式.这也是在实际工作中用到最多的方式. 下面先给出一些目录: 1.什么是Co ...

  3. 【EF Code First】 一对一、一对多的多重关系配置

    这里使用相册Album和图片Picture的关系做示例 1,Album与Picture最基本的关系是1-n(一个相册可以有多张图片) 这时Album.Picture实体类可以这么定义 /// < ...

  4. EF Code First学习笔记 初识Code First

    Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库. 下面通过一个简单的示例来 ...

  5. EF Code First学习笔记 初识Code First(转)

    Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库. 下面通过一个简单的示例来 ...

  6. EF Code First 学习笔记:关系

      一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就能通过一些引用属性.导航属性等检测到模型之 ...

  7. EF Code First 学习笔记:关系(转)

      一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就能通过一些引用属性.导航属性等检测到模型之 ...

  8. EF Code-First 学习之旅 配置一对一的关系

    1对1.1对0 的关系 例如:Entity1与零个或一个Entity2的实例有关系 public class Student { public Student() { } public int Stu ...

  9. EF Code First 学习笔记:表映射

    多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Perso ...

随机推荐

  1. 《Sqlserver》通过端口 8080 连接到主机 localhost 的 TCP/IP 连接失败。错误:“驱动程序收到意外的登录前响应。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受

    1. 点击 开始 --> 所有程序 --> Microsoft SQL Server2005 --> 配置工具-->SQL Server configuration Manag ...

  2. centos7的nfs配置

    author : headsen chen date : 2018-04-12  09:40:14  一,服务端安装和配置: 环境准备: systemctl stop firewalld system ...

  3. 【微信】QQ邮箱助手不提醒解决

    1.问题及原因: 微信上的QQ邮箱提醒功能失效了,收到信后不提醒.忘了是使用了TIM,还是使用了QQ邮箱之后才出现的. 不过原因是:QQ邮箱提醒被设置为仅在QQ邮箱客户端提醒 2.解决: 重新安装QQ ...

  4. 巨蟒python全栈开发-第14天 内置函数2 递归 二分查找

    一.今日内容总览 1.内置函数补充 repr() 显示出字符串的官方表示形式 chr() arscii码中的字,转换成位置 ord() arscii码中的位置,转换成字2.递归 自己调用自己 两个口: ...

  5. JavaScript 学习(1)--window对象

    JavaScript 学习--Window对象 window对象方法 1.1 创建新窗体 //窗体打开 var newWindow=Window.open("default.html&quo ...

  6. Java中native关键字使用

    native是与C++异构开发的时候用的.java自己开发不会使用

  7. MyBatis 之动态SQL

    1. 概述 动态 SQL 极大的简化了我们拼装SQL的操作; MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作: if choose(when,otherwise) trim(wher ...

  8. FastReports_4.14.1 _Cliff手动安装

    首次编译frx15.dproj包的时候,会出错: [DCC Fatal Error] fs15.dpk(59): F1026 File not found: 'fs_ipascal.dcu'原因是因为 ...

  9. 洛谷 P1641 [SCOI2010]生成字符串

    洛谷 这题一看就是卡塔兰数. 因为\(cnt[1] \leq cnt[0]\),很显然的卡塔兰嘛! 平时我们推导卡塔兰是用一个边长为n的正方形推的, 相当于从(0,0)点走到(n,n)点,向上走的步数 ...

  10. Python(数据库之约束表的关系)

    一.约束 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性 主要分为: RIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...