Search code examples
c#entity-frameworkmany-to-manycode-firstdbcontext

Code first many to many relationship with attributes in relational table


I want to create relation attributes with relational table. Below is the referance code. But the code create 2 tables BookStudents and BookStudents1. where one have relation attributes without forigen keys and other creates table without attributes but with forign keys. How can I solve this issue?

public class BookStudent
{
    [Key, Column(Order = 0)]
    public int BookID { get; set; }
    [Key, Column(Order = 1)]
    public int StudentID { get; set; }
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
}


public class Context : DbContext
{
    public Context() : base("name=DefaultConnection") { }
    public DbSet<Book> Books { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<BookStudent> BookStudents { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Book>().HasMany<Student>(t => t.Students).WithMany(t => t.Books).Map(t =>
        {
            t.MapLeftKey("BookId");
            t.MapRightKey("StudentId");
            t.ToTable("BookStudents");
        });
    }
}

Solution

  • In code first many cases we do not need to define explicitly relationship on OnModelCreating. Use below code this will solve your problem.

    public class BookStudent
    {
        [Key, Column(Order = 0)]
        public int BookID { get; set; }
        [Key, Column(Order = 1)]
        public int StudentID { get; set; }
        public DateTime FromDate { get; set; }
        public DateTime ToDate { get; set; }
    
        //below two lines will define foreign key
        public Student Student { get; set; }
        public Book Book { get; set; }
    }
    
    
    public class Context : DbContext
    {
        public Context() : base("name=DefaultConnection") { }
        public DbSet<Book> Books { get; set; }
        public DbSet<Student> Students { get; set; }
        public DbSet<BookStudent> BookStudents { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //remove below code
            //modelBuilder.Entity<Book>().HasMany<Student>(t => t.Students).WithMany(t => t.Books).Map(t =>
            //{
            //    t.MapLeftKey("BookId");
            //    t.MapRightKey("StudentId");
            //    t.ToTable("BookStudents");
            //});
        }
    }