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. hdu2732 (Leapin' Lizards)

    题目链接:传送门 题目大意:给你 n,m  n:有几行图,m是一个人最多跳m个曼哈顿距离. 给你两张图,第一张图数字为0表示没有柱子,否则有柱子且只能跳出去 x 次(x为当前字符代表的数字) 第二张图 ...

  2. IE、FF脚本兼容性问题

    1.window.event IE有这个对象:FF没有,FF通过参数传递 2.获取事件源 IE:srcElement FF:target 3.添加与去除事件 IE:element.attachEven ...

  3. ubuntu设置开机启动脚本

    rc.local脚本 rc.local脚本是一个ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令.该脚本位于/etc/路径下,需要root权限才能修改. 该脚本具体格式如下: #! ...

  4. 巨蟒python全栈开发数据库攻略2:基础攻略2

    1.存储引擎表类型 2.整数类型和sql_mode 3.浮点类&字符串类型&日期类型&集合类型&枚举类型 4.数值类型补充 5.完整性约束

  5. redis数据库设计(转)

    原文:http://segmentfault.com/q/1010000000316112 redis是什么 redis就是一个存储key-value键值对的仓库,如何使用redis在于如何理解你需要 ...

  6. 白话Redis分布式锁

    redis分布式 简单来说就是,操作redis实例时,不是常规(单机)操作一个实例,而是操作两台或多台,也就是基于分布式集群: 而且redis内部是单进程.单线程,是数据安全的(只有自己的线程在操作数 ...

  7. Submission Details [leetcode] 算法的改进

    最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...

  8. java中jdk安装配置信息

    由于被人比较懒,有些做过的事情总是好忘,这个也不例外,索性就做个随笔. JAVA_HOMEC:\Program Files (x86)\Java\jdk1.7.0_03CLASSPATH.;%JAVA ...

  9. python16_day02【列表、字典】

    1.列表 names = ['Alex',"Tenglan",'Eric'] >>> names[0] 'Alex' >>> names[2] ...

  10. go——接口(二)

    多态是指代码可以根据类型的具体实现采取不同行为的能力. 如果一个类型实现了某个接口,所有使用这个接口的地方,都可以支持这种类型的值. 标准库里有很好的例子,如io包里实现的流式处理接口. io包提供了 ...