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

我怕爱的太早我们不能终老 提交于 2019-12-11 07:53:37

问题


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 wich 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);
 }

回答1:


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; }


来源:https://stackoverflow.com/questions/53738311/how-to-remove-many-to-many-auto-generated-table-with-entity-framework-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!