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