Reputation: 1271
I have something which i don't understand. This is a similar case like mine. I have BaseEntity, Product, Supplier, Contract and a ProductSupplierForContract Entities, all inherit from BaseEntity.
Base Entity:
public class BaseEntity
{
public int ID { get; set; }
// other properties that are not entities by themself
}
Product Entity:
[Required]
public ICollection<Supplier> Suppliers { get; set; }
[Required]
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
public ICollection<Contract> Contracts { get; set; }
Supplier Entity:
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
public ICollection<Product> Products { get; set; }
ProductSupplierForContract Entity:
public string ProductnumberValue { get; set; }
public Supplier Supplier { get; set; }
public int Supplier_Id { get; set; }
public Product Product { get; set; }
public int Product_Id { get; set; }
public Contract Contract { get; set; }
public int? Contract_Id { get; set; }
Contract Entity:
[Required]
public ICollection<Product> Products { get; set; }
public ICollection<Supplier> Suppliers { get; set; }
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
Following scenario must be possible and allowed: 1 Contract can hold multiple instances of 1 product BUT from a different supplier. Therefore i created the ProductSupplierForContract (PSFC) Entity which would hold this relation of 1product-1supplier-1productnumber (value) -1contract .
When i edit 1 existing product that already has 1 PSFC instance, and add another instance of PSFC holding another PK, ProductId & SupplierId, i get this error:
{"Multiplicity constraint violated. The role 'ProductSupplierForContract_Product_Target' of the relationship 'ContractCare.Models.ProductSupplierForContract_Product' has multiplicity 1 or 0..1."}
I don't get why because isn't it so that i can have:
PSFC 1
PK 1
ProductId 1
SupplierId 1
PSFC 2
PK 2
ProductId 1
SupplierId 2
Why should i (like described in the linked post above) have a many to many relation PSFC on Products?
Thank you for any feedback! Kind regards.
Upvotes: 0
Views: 3370
Reputation: 1271
Extending on the link that Aparna Gadgil provided, one of the comments posted by Arturo Hernandez resolved the error that i had so i am going to mark this solved since my original issue was the error.
Quote:
The problem is that detachedChild.parent should be assigned attachedParent.
foreach(var detachedEntity in detachedEntities)
{
attachedEntity.ChildEntities.Add(detachedEntity);
detachedEntity.ParentEntity = attachedEntity;
}
In my case, i had to attach the parent entity (product we are updating) to the ProductSupplierForContract.Product. Creating the new entity like suggested in the answer was already the case for me, and other things like attaching it and so on didn't fixed it for me.
Since i didn't post any code on how i was performing this action this wasn't very helpful to be able to help me and i will surely try to be more specific in the future :).
Kind regards and thank you!
Upvotes: 1