locked
HOW TO CREATE MANY TO MANY RELATIONSHIP UISNG CODE FIRST APPROACH IN MVC RRS feed

  • Question

  • User569149469 posted

    hi,

    I have two classes like this deal and product want to create a  many to many relatioship followed this approach but getting error any help is appreciated

     public class Deal 
        {
            public int id { set; get; }
    
            public string Deal_Sku { get; set; }
    
            public string Deal_Name { get; set; }
    
            public decimal cost { get; set; }
    
            public  IList<DealProduct> DealProducts{ get;set;}
        }
    
     public class Product
        {
            public int ID { get; set; }
            public string Prod_SKU { get; set; }
            public string Prod_Name { get; set; }
            public double Price { get; set; }
            public  IList<DealProduct> DealProducts { get; set; }
    
        }
     public class DealProduct
        {
            public int DealId;
            public int ProductId;
            public Decimal Cost;
            public bool Free;
        }
    
    I am trying to create codefirst like this but getting error
      public  DbSet<Product> Products { get; set; }
           
            public  DbSet<CartItem> CartItems { get; set; }
    
            public  DbSet<Deal> Deals { get; set; }
    
            public  DbSet<DealProduct> DealProducts { get; set; }
           
            
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
                //modelBuilder.Entity<DealProduct>()
                //     .HasKey(c => new { c.DealId, c.ProductId });
                //modelBuilder.Entity<Deal>()
                //    .HasMany(d => d.DealProducts)
                //    .WithRequired()
                //    .HasForeignKey(c => c.DealId);
    
                //modelBuilder.Entity<Product>()
                //    .HasMany(p => p.DealProducts)
                //    .WithRequired()
                //    .HasForeignKey(d => d.DealId);
                            
            }
    

    Friday, January 11, 2019 1:24 AM

Answers

  • User1520731567 posted

    Hi phmaspnet,

    Actually,you just add Deal and Product model,EF automatically creates a joining table (DealProduct) with the name of the both entities and the suffix 's'.

    EF 6 includes default conventions for many-to-many relationships .You need to include a collection navigation property at both ends without any configuration. 

    For example:

     public class Deal 
        {
    public Deal()
    {
    this.Products= new HashSet<Product>();
    } public int id { set; get; } public string Deal_Sku { get; set; } public string Deal_Name { get; set; } public decimal cost { get; set; } public IList<Product> Products{ get;set;}//navigation property } public class Product {
    public Product()
    {
    this.Deals= new HashSet<Deal>();
    } public int ID { get; set; } public string Prod_SKU { get; set; } public string Prod_Name { get; set; } public double Price { get; set; } public IList<Deal> Deals{ get; set; }//navigation property } public class DealProduct { public int DealId; public int ProductId; public Decimal Cost; public bool Free; }
      public  DbSet<Product> Products { get; set; }
           
            public  DbSet<CartItem> CartItems { get; set; }
    
            public  DbSet<Deal> Deals { get; set; }
    
            public  DbSet<DealProduct> DealProducts { get; set; }

    And then add-migration and update-database.

    The default conventions for many-to-many relationships creates a joining table with the default naming conventions.

    You could use Fluent API to customize a joining table name and column names.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    
        modelBuilder.Entity<Deal>()
                    .HasMany<Product>(s => s.Products)
                    .WithMany(c => c.Deals)
                    .Map(cs =>
                            {
                                cs.MapLeftKey("DealId");
                                cs.MapRightKey("ProductId");
                                cs.ToTable("DealProduct");
                            });
    
    }

    More details,,you could refer to this article,here is the Tutorial about Configure Many-to-Many Relationships in Code-First:

    http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 11, 2019 8:31 AM