原文链接:https://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

EF 6 Code-First系列文章目录:

这里,我们将学习如何在Student实体和Course实体间配置多对多关系,Student可以参加很多Courses,并且多个学生可以加入同一个Course。

可以看下这篇文章, Entity Relationship 了解一下EF是如何管理实体间的一对一,一对多以及多对多关系的。

通过默认约定配置多对多关系

EF 6包含多对多关系的默认约定,你需要在两个实体间都包含集合类型的导航属性。例如:Student类应该包含一个集合类型的导航属性Course,同样Course类也应该包含一个集合类型的导航属性Student:

public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
} public int StudentId { get; set; }
[Required]
public string StudentName { get; set; } public virtual ICollection<Course> Courses { get; set; }
} public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
} public int CourseId { get; set; }
public string CourseName { get; set; } public virtual ICollection<Student> Students { get; set; }
}

下面的上下文类中,包含Student和Course实体:

public class SchoolDBContext : DBContext
{
public SchoolDBContext() : base("SchoolDB-DataAnnotations")
{
} public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}

EF API将会创建Students表和Courses表,同样还会创建联接表StudentCourses,StudentCourses表中,包含两个表中的主键作为主键以及外键:

enter description here

注意:联接表的名称就是两个实体名称+后缀s.

使用Fluent API配置多对多关系

上面的例子中,你已经看到了默认的约定为我们创建了多对多关系的表,以及相关的联接表。我们可以使用FLuent API来配置连接表的名称和列。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{ modelBuilder.Entity<Student>()
.HasMany<Course>(s => s.Courses)
.WithMany(c => c.Students)
.Map(cs =>
{
cs.MapLeftKey("StudentRefId");
cs.MapRightKey("CourseRefId");
cs.ToTable("StudentCourse");
}); }

上面的代码中,HasMany()方法和WithMany()方法,用来给Student和Course实体配置多对多关系。Map()方法包含一个Action类型的委托,这里我们传入lambda,来定制联接表。MapLeftKey()用来指定Student表中的主键名称(因为我们从Student实体开始配置,所以Student是左表),MapRightKey()用来配置Course表中的主键名称,ToTable用来指定联接表的名称。

enter description here

这样你就通过Fluent API重写了默认约定,配置了多对多关系。

13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】的更多相关文章

  1. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  2. 11.Configure Many-to-Many(配置多对多关系)【Code-First系列】

    现在学习EF Code-First多对多的配置. 这里我们举例:学生和班级实体,一个学生可以选修多个课程,多个学生也可以选修同一个课程. 一.使用数据注解特性,配置多对多的关系 using Syste ...

  3. CI Weekly #13 | 用更 Geek 的方式配置你的 CI 工作流

    flow.ci 的重大更新来了--支持通过 .yml 文件配置工作流(测试阶段),具体的使用方法可参考文档:同时 flow.ci 也开放了社区>> club.flow.ci,使用的任何问题 ...

  4. 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored ...

  5. 1 翻译系列:什么是Code First(EF 6 Code First 系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...

  6. 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...

  7. 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...

  8. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  9. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

随机推荐

  1. leetcode979

    搞不定这种递归计算,可能我的头脑是“线性”的,这种一层一层的,想起来太费劲了,想的头发都没了.以后希望能有AI来写这种程序吧,AI不怕掉头发! class Solution(object): def ...

  2. JS 正则表达式基本语法(精粹)

    1.正则表达式基本语法 两个特殊的符号'^'和'$'.他们的作用是分别指出一个字符串的开始和结束. 例子如下: "^The":表示所有以"The"开始的字符串( ...

  3. 337. House Robber III二叉树上的抢劫题

    [抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  4. LAB4

    鼠标放上去可以看文件说明,exception则在exception中处理 文件头里面穿信息,为404 out.flush();把以前的全部冲掉了,所以需要放在后面

  5. 加NONCLUSTERED INDEX索引,在ON了之后还要INCLUDE

    之前加了索引,但效果不大 SET STATISTICS TIME ON --执行时间 SET STATISTICS IO ON --IO读取 DBCC DROPCLEANBUFFERS --清除缓冲区 ...

  6. 优秀的 Spring Cloud 开源软件

    Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路器.数据监控等,都可以用 ...

  7. CentOS Linux下VNC Server远程桌面配置详解

    http://www.ha97.com/4634.html PS:偶以前基本不用Linux的远程图形桌面,前几天有开发的同事配置CentOS的vnc有问题,找我解决,就顺便记录总结一下,这个总结是比较 ...

  8. Linux驱动之异步通知的应用

    前面的按键驱动方式都是应用程序通过主动查询的方式获得按键值的: 1.查询方式 2.中断方式 3.poll机制 下面介绍第四种按键驱动的方式 4.异步通知:它可以做到应用程序不用随时去查询按键的状态,而 ...

  9. maven 监理web war 项目

  10. robotframework手机号随机产生脚本

    首先,要导入使用库 random; ${phone} Evaluate random.choice(['139','188','185','136','158','151'])+"" ...