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. GUI设计和UI设计有什么区别?

    首先从技术的角度分析两者处于包含与被包含的关系. GUI=Graphical User Interface,是指在计算机出现后,在屏幕上使用图形界面来帮助(User)与机器打交道用的界面接口,泛指在计 ...

  2. ABP Xunit单元测试 第五篇

    1.创建如下的项目结构 public class TestName { public bool ValidateName(string Name) { if (Name == "yin&qu ...

  3. MSSQL语句学习(查询表的总记录数)

    如何高效查询表的总记录数(通用方法) SELECT COUNT(1) ROWS FROM product 野路子1:利用系统自带的存储过程SP_SPACEUSED,详细的使用方式推荐谷哥或度娘, EX ...

  4. Alpha项目冲刺

    一.团队成员 学号 姓名 211606361 何承华(队长) 211606356 陈宇 211606360 丁培辉 211606333 温志铭 211606343 杨宇潇 211606391 张主强 ...

  5. crontab定时时间解释

    用户所建立的crontab文件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下: minute hour day mo ...

  6. Ubuntu 14.04 LTS 下使用源码编译安装 Sagemath 6.7 x64 (小结)

    原先博客放弃使用,几篇文章搬运过来 下载源码包 系统的最低要求: 6GB 硬盘 : 2GB RAM. 命令行工具: A C/C++ compiler: Since Sage builds its ow ...

  7. MFC:Tab控件嵌入对话框

    1.先建立一个对话框MFC应用程序,然后在工具箱里面把Tab Control控件放到对话框中的合适位置上. 再在对话框类中,声明一个CTabCtrl变量: CTabCtrl m_tab; 变量m_ta ...

  8. Learning Rust - Syntax

    Rust is another compiling language that may replace the position of C/C++ in server filed. It runs f ...

  9. Django的一些隐性经验

    隐性经验 前后信息的沟通 url中的参数 get获取 这个参数可以写在URL当中(可以写多个,写在这里的get函数需要有相应的参数去获取).,也可以在模版中添加(通过?若是直接写则表示在当前的URL中 ...

  10. EF生成实体自动添加数据库字段注释

    我们在用EF从数据库生成模型的时候,默认实体类是没有注释的,但是我们已经在数据库字段添加说明了,能不能自动把注释也拿过来? 答案是:能. 那么我们开始 首先随便开一个ASP.NET   MVC项目,我 ...