How to represent Bridge table in Entity Framework Code First

ⅰ亾dé卋堺 提交于 2019-11-30 18:40:47

In short: This relationship type between Student and Class is called many to many relationship in EF terminology.

Entity framework handles tables participating in Many to Many relationship in a nice manner. If the junction table (sometimes called bridge table, association table, link table, etc) only consists of the foreign keys and no other columns, then that table is abstracted by EF and the two sides get a navigational property exposing a collection of the other side.

What you would need is to generate a Entity Model for the above tables first or define your code first structure like in the following post - Creating a Many To Many Mapping Using Code First.

Basically, depending on your structure you need to set the method OnModelCreating appropriate to the relationships in your classes.

protected override void OnModelCreating(DbModelBuilder modelBuilder)

Depending on your query needs you will find it informative to look at the following references:

There are many ways to represent many-to-many relationships in Code First. It really just depends on what your needs are with the mappings. If you simply need to register the map everything you have works so far. The relationship you are showing will be recognized by Code First and a mapping table will automatically be created for you called "StudentClasses"

To create a mapping in the mapping table simply do the following:

using(var context = new StudentClassContext())
{ 
     Student aStudent = new Student{ StudentName="Johnny", Class = new Class(){ ClassName="Gym"}}
     context.Students.Add(aStudent);
     context.SaveChanges();
}

You can also do this in reverse:

using(var context = new StudentClassContext())
{ 
     Class aClass = new Class{ ClassName = "Gym", Students = new Student(){ StudentName="Johnny"}}
     context.Classes.Add(aClass);
     context.SaveChanges();
}

If you want to be more specific and explicit about the relationship you can setup an explicit mapping in the overridden context OnModelCreating() method. That would look like this:

public class StudentClassContext
{
    public DbSet<Student> Students { get; set; }
    public DbSet<Class> Classes { get; set; }

    public StudentClassContext()
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>()
           .HasMany(aStudent => aStudent.Classes) // A student can have many classes
           .WithMany(aClass => aClass.Students)); // Many classes can have many students
         //.Map(c => c.ToTable("StudentClass); // Set the mapping table to this name
    }

}

If for some reason you need to access the mapping table in code or outside of the EntityFramework context operations I would say create a dedicated map class and make it a first class citizen in your code:

public class Student
{
    //...
}


public class Class
{
    //...
}

// New Map Class
public class StudentClassMapRecord
{
    public int Id; // Map record PK
    public Student Student { get; set; }
    public Class Class { get; set; }

    public StudentClassMapRecord(Student aStudent, Class aClass)
    {
        Student = aStudent;
        Class = aClass;
    }

    //Uncomment below if you don't need property navigation
    //public int StudentId { get; set; }
    //public int ClassId { get; set; }        
    //public StudentClassMapRecord(int studentId, int classId)
    //{
    //    StudentId = studentId;
    //    ClassId = classId;
    //} 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!