我有3个实体产品,供应商和合同,产品和供应商各自具有彼此的引用作为参考,从而创建了一个ProductSupplier表。
要在此多对多关系上添加额外的属性,我创建了第三个实体 ProductForSupplier ,其中包含1个产品,1个供应商和一个附加字符串Property ProductNumber。
此外,我还创建了另一个实体 ProductSupplierForContract ,其中包含一个ProductForSupplier和一个合同。当我播入一些数据进行测试时,我可以观察到ProductSupplierForContract在我的ProductForSupplier实体中存在时没有产品和供应商ID值,但是id确实有记录的ProductForSupplierId。
由于我具有包含这2个值的表的ID,我如何在表ProductSupplierForContract中删除这2个属性?
实体:
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; }
}
播种方法
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);
}
答案 0 :(得分:1)
对不起,
我在我的 Supplier 和 Product 实体中都删除了 ProductSupplierForContract 参考,这给了我我想要的东西,因为那是创建它们的原因。
在两个实体中都删除了此行:
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }