How to represent Bridge table in Entity Framework Code First

前端 未结 2 1621
刺人心
刺人心 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: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 Students { get; set; }
        public DbSet Classes { get; set; }
    
        public StudentClassContext()
        {
    
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity()
               .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;
        //} 
    }
    

提交回复
热议问题