1

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);
 }
Andrew Nepogoda
  • 1,737
  • 16
  • 24
Dimitri
  • 1,075
  • 2
  • 14
  • 35

1 Answers1

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; }
Dimitri
  • 1,075
  • 2
  • 14
  • 35