How to represent Bridge table in Entity Framework Code First

前端 未结 2 1622
刺人心
刺人心 2021-01-04 07:58

I am trying to find out how I can represent a bridge table between two entities (many to many relation) I\'m Using Entity Framework Code First

Student:
   St         


        
相关标签:
2条回答
  • 2021-01-04 08:33

    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:

    • Queries involving many to many relationship tables
    • Many To Many Mappings in Entity Framework
    • Many-to-Many Relationships with Entity Framework
    0 讨论(0)
  • 2021-01-04 08:59

    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;
        //} 
    }
    
    0 讨论(0)
提交回复
热议问题