原文链接: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. php获取微信的openid

    https://www.cnblogs.com/wxfallstar/p/6826886.html https://www.cnblogs.com/liangxiblog/p/5909432.html

  2. 深度学习原理与框架-Tfrecord数据集的制作 1.tf.train.Examples(数据转换为二进制) 3.tf.image.encode_jpeg(解码图片加码成jpeg) 4.tf.train.Coordinator(构建多线程通道) 5.threading.Thread(建立单线程) 6.tf.python_io.TFR(TFR读入器)

    1. 配套使用: tf.train.Examples将数据转换为二进制,提升IO效率和方便管理 对于int类型 : tf.train.Examples(features=tf.train.Featur ...

  3. oracle数据链接

    using System; using System.Collections.Generic; using System.Data; using System.Data.OracleClient; u ...

  4. 关于IE 浏览器的position居中定位的问题和 行块元素的设置问题

    这两天在写页面时,遇到一些IE浏览器显示不正常的问题,主要有两个: 1. 在td 中设置span 元素水平垂直居中,在谷歌浏览器中可以正常显示,但是在IE 中却无法显示出想要的结果,即不能实现垂直水平 ...

  5. week07 13.4 NewsPipeline之 三 News Deduper

    还是循环将Q2中的东西拿出来 然后查重(去mongodb里面把一天之内的新闻都拿出来,然后把拿到的新的新闻和mongodb里一天内的新闻组一个 tf-idf的对比)可看13.3 相似度检查 如果超过一 ...

  6. Mesh属性[Unity]

    Mesh属性[Unity] Mesh是Unity内的一个组件,称为网格组件.3D网格是Unity中最重要的图形元素.在Unity中存在多种组件用于渲染标准网格或者蒙皮网格.拖尾或者3D线条. 在Uni ...

  7. Django 学生信息 添加 功能 遇到的问题.

    1  添加 班级信息时的问题 (grade为外键) 原因是 grade 必需接收 一个 实例, 而我交是一个 str字符串, if request.method == 'POST': data = { ...

  8. dede织梦动态页面通过手机模板实现wap浏览

    https://jingyan.baidu.com/article/a948d6517be0eb0a2dcd2ebc.html

  9. PowerScript语言基础

    注释: 以 "//" 开头,其后书写注释内容,常用于单行注释. "/-/"中间的部分为注释,便于多行说明. //这是一个单行注释 INTEGER I I = I ...

  10. PHP Laravel定时任务Schedule

    前提:本文方法是利用Linux的crontab定时任务来协助实现Laravel调度(Mac也一样). 一.首先添加Crontab定时任务,这里只做简单介绍. 用命令crontab -e 添加如下内容 ...