Configure Many-to-Many relationship:

Here, we will learn how to configure Many-to-Many relationship between the Student and Course entity classes. Student can join multiple courses and multiple students can join one course.

Visit Entity Relationship section to understand how EF manages one-to-one, one-to-many and many-to-many relationships between the entities.

Configure Many-to-Many relationship using DataAnnotation:

Student class should have a collection navigation property for Course, and Course should have a collection navigation property for student, which will create a Many-to-Many relationship between student and course as shown below:

public class Student
{
public Student() { } public int StudentId { get; set; }
[Required]
public string StudentName { get; set; } public int StdandardId { 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; }
}

The code shown above will create the following database, where Code-First will create a third joining table, CourseStudent, which will consist of the PK of both the tables, i.e. StudentId & CourseId:

Configure Many-to-Many relationship using Fluent API:

You can use the Fluent API to configure a Many-to-Many relationship between Student and Course, as shown below:

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");
}); }

As you can see in the above example, .HasMany<Course>(s => s.Courses).WithMany(c => c.Students) says that Student and Course has many-to-many relationship with Students navigation property in Course class and Courses navigation property in Student class.

Map method takes Action type delegate, hence, we can pass lambda expression wherein we will specify FK property name of Student (we start with Student entity, so it will be left table) and FK of Course table. ToTable will create StudentCourse table.

This will create a new joining table StudentCourse with two Primary Keys which will also be Foreign Keys, as shown below:

Configure Many-to-Many relationship:的更多相关文章

  1. 国产深度学习框架mindspore-1.3.0 gpu版本无法进行源码编译

    官网地址: https://www.mindspore.cn/install 所有依赖环境 进行sudo make install 安装,最终报错: 错误记录信息: cat     /tmp/mind ...

  2. 9.Configure One-to-One(配置一对一关系)【Code-First系列】

    现在,开始学习怎么配置一对一的关系,众所周知,一对一的关系是:一个表中的主键,在另外一个表中,同时是主键和外键[实际上是一对零或者一对一]. 请注意:一对一的关系,在MS SQL Server中,技术 ...

  3. 10.Configure One-to-Many(配置一对多关系)【Code-First系列】

    现在,我们将学习怎么配置一对多的关系. Visit Entity Relationship section to understand how EF manages one-to-one, one-t ...

  4. How to Configure the Gradient Boosting Algorithm

    How to Configure the Gradient Boosting Algorithm by Jason Brownlee on September 12, 2016 in XGBoost ...

  5. "No appenders found for logger" and "Please configure log4j properly"

    Why do I see a warning about "No appenders found for logger" and "Please configure lo ...

  6. Entity Framework Code-First(13):Configure Many-to-Many

    Configure Many-to-Many relationship: Here, we will learn how to configure Many-to-Many relationship ...

  7. Entity Framework Code-First(12):Configure One-to-Many

    Configure One-to-Many Relationship: Here, we will learn how to configure One-to-Many relationship be ...

  8. Entity Framework Code-First(11):Configure One-to-One

    Configure One-to-Zero-or-One Relationship: Here, we will configure One-to-Zero-or-One relationship b ...

  9. Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7

    SOLUTION VERIFIED September 13 2016 KB1248793 Environment Red Hat Enterprise Linux 7 NetworkManager ...

随机推荐

  1. xpath&css选择器

    本文参考较多,原创基本没有,权当知识归纳. xpath并不复杂,简单的使用看完之后,及时查阅文档也是可以写出来的. 这里放上我的练手文件,大家可以参考,或者挑毛病(*^__^*) 嘻嘻-- xpath ...

  2. Sort Array By Parity II LT922

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

  3. ManageEngine SeviceDesk Plus帮助台和资产管理软件

  4. ABP框架系列之二十:(Dependency-Injection-依赖注入)

    What is Dependency Injection If you already know Dependency Injection concept, Constructor and Prope ...

  5. ASP.NET Core 简单实现七牛图片上传(FormData 和 Base64)

    ASP.NET Core 简单实现七牛图片上传(FormData 和 Base64) 七牛图片上传 SDK(.NET 版本):https://developer.qiniu.com/kodo/sdk/ ...

  6. mac下安装安卓开发环境

    对于做ios的人来说,安装安卓开发环境,最好是在mac下安装了,我的mac是10.8.2,64位系统的 安卓开发环境需要下面几个东西: 1 jdk(mac下已经默认有了,可以在命令提示符下输入java ...

  7. 【LOJ6036】 「雅礼集训 2017 Day4」编码

    传送门 LOJ Solution 因为?只有两种可能为0,1,所以就把这两个串搞出来. 那么现在?取0和?取1不能并存,前缀不能并存,所以就是一个\(2-SAT\),现在问题在于这个东西可能会有很多条 ...

  8. liunx----配置搜狗输入法

    话不多说直接看步骤 * 系统版本: ubuntu 18.04.1 // 查看命令为: # cat /etc/issue 1. 先查看当前系统是否存在 fcitx 框架: # dpkg -l | gre ...

  9. Android框架式编程之MVP架构

    MVP(Model-View-Presenter)模式.是将APP的结构分为三层:View - Presenter - Model. View 1. 提供UI交互 2. 在presenter的控制下修 ...

  10. ios webview下footer部分fixed失效问题

    场景: 如下图所示,一个很正常的页面需求,footer固定在底部,中间为滚动内容区 然后footer的css一般是这样的 footer{ position:fixed; bottom:; left:; ...