Search code examples
asp.net-mvcentity-framework-6ef-code-firstrelational-database

How to remove Many to Many auto generated table with Entity Framework Code First


I have 3 Entities Product, Supplier & Contract, Product & Supplier each have as reference a Collection of each other which creates a ProductSupplier Table.

To add an extra property on this Many to Many relation I have created a 3rd Entity ProductForSupplier which holds 1 Product, 1 Supplier and a extra string Property ProductNumber.

In addition I have created another Entity ProductSupplierForContract which holds a ProductForSupplier & a Contract. When I seed some data for test I can observe that the ProductSupplierForContract doesn't have the product & Supplier Id value while they are present in my ProductForSupplier Entity but id does have the ProductForSupplierId of the record.

How can I remove these 2 properties in the table ProductSupplierForContract since I have the Id of the table holding these 2 values?

Entities:

public class Product : BaseEntity // BaseEntity just holds an Id and a date
{
    public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
    public ICollection<ProductForSupplier> ProductForSuppliers { get; set; }
}

public class Supplier : BaseEntity
{
    public ICollection<ProductForSupplier> ProductForSuppliers { get; set; }
    public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
} 

public class Contract : BaseEntity
{
    public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
}

public class ProductForSupplier:BaseEntity
{
    public string ProductNumber{ get; set; }

    [Required]
    public Product Product { get; set; }

    [Required]
    public Supplier Supplier { get; set; }
}

public class ProductSupplierForContract: BaseEntity
{
    [Required]
    public ProductForSupplier ProductForSupplier { get; set; }

    [Required]
    public Contract Contract { get; set; }
}

Seeding method

 protected override void Seed(TestDbContext context)
 {
     Supplier supplier1 = new Supplier("Microsoft");
     context.Suppliers.Add(supplier1);         

     Product product1 = new Product("test product 1");
     context.Products.Add(product1);

     Contract contract = new Contract("Contract 1");
     context.Contracts.Add(contract);

     ProductForSupplier pfs = new ProductForSupplier("123productNumber");
     pfs.Supplier = supplier1;
     pfs.Product = product1;        
     context.ProductForSuppliers.Add(pfs);

     ProductSupplierForContract psfc = new ProductSupplierForContract(pfs, contract);
     context.ProductSupplierForContracts.Add(psfc);

     base.Seed(context);
 }

Solution

  • Silly me,

    I removed the ProductSupplierForContract reference in both my Supplier & Product Entity and that gave me what i want since that was what created these.

    Removed this line in both Entities:

    public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }