专栏首页c#开发者Configure Many-to-Many relationship:

Configure Many-to-Many relationship:

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:

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 跨库的查询策略

    对拆分字段的查询 单值查询 select * from table1 where user_id=‘test1234’ user_id 是分库时的拆分字段...

    dys
  • 分库后如何处理分页?

    在数据量过大以后,通常都会进行分库操作,把一张表拆分到不同数据库中 例如 tb1 表被拆分到3个库中,分库1、分库2、分库3 现在想执行分页操作 SELECT ...

    dys
  • 使用Redis统计活跃用户

    统计活跃用户这个案例非常经典,也是我当时学习redis时,接触到的第一个让我眼睛一亮的使用方式 场景 用户登录后需要记录,以便以后进行登录统计 统计需求主...

    dys
  • mysql基本命令-基础教程(二)

    用得最多的就是对数据的 增、删、改、查; 首先说明: 具体数据是存在表里面(这个东西可以想象一下excel表格); 表又存在数据库; 一个mysql软件里面可...

    前朝楚水
  • 干货 | 传奇技术大师吕海波:Oracle中最容易被忽略的那些实用特性

    嘉宾演讲视频 Guest Video ? 温馨提示 本视频时长55分04秒,建议在wifi下观看 4月8日,由杭州市经信委指导,DBGeeK社群、Postgre...

    IT大咖说
  • mongodb 性能篇

    一、  索引及其优化 索引的概述 数据库的索引好比是一本书前面的目录,能加快数据查询的速度。 适当的地方增加索引,不合理的地方删除次优索引,能优化性能较差的应用...

    一夕如环
  • 必懂的NoSQL理论-Map-Reduce(上)

    本文主要内容:基本的Map-Reduce Map-Reduce 基本原理 面向聚合的数据库能够兴起很大一部分原因是由于集群的增长。数据库运行在集群环境中意...

    ImportSource
  • mongodb架构篇

    一、  复制集 Replica set的架构 Replica set的部署 主从操作日志 在local数据库里面:db.oplog.rs.find() 日志的基...

    一夕如环
  • ACMUG 2016My SQL全球开源数据库年会

    关于中国MySQL用户组 MySQL 是全球范围内最流行的开源数据库。其稳定,高效,易用使其成为互联网应用之首选数据库平台。被Facebook、Twitter、...

    IT大咖说
  • mysql命令之对用户,库,表操作--基础教程(三)

    创建用户: 进入在root用户进行操作: CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456'; 授权...

    前朝楚水

扫码关注云+社区

领取腾讯云代金券