违反多重约束 Entity framework 6
Multiplicity constraint violated Entity framework 6
我有一些我不明白的地方。 This 和我的情况类似。
我有 BaseEntity、Product、Supplier、Contract 和 ProductSupplierForContract 实体,它们都继承自 BaseEntity。
基础实体:
public class BaseEntity
{
public int ID { get; set; }
// other properties that are not entities by themself
}
产品实体:
[Required]
public ICollection<Supplier> Suppliers { get; set; }
[Required]
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
public ICollection<Contract> Contracts { get; set; }
供应商实体:
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
public ICollection<Product> Products { get; set; }
ProductSupplierForContract 实体:
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; }
合同实体:
[Required]
public ICollection<Product> Products { get; set; }
public ICollection<Supplier> Suppliers { get; set; }
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
以下场景必须是可能的并被允许:
1 个合同可以包含来自不同供应商的 1 个产品的多个实例。
因此,我创建了 ProductSupplierForContract (PSFC) 实体,它将保持 1product-1supplier-1productnumber (value) -1contract .
的这种关系
当我编辑 1 个已有 1 个 PSFC 实例的现有产品,并添加另一个 PSFC 实例持有另一个 PK、ProductId 和 SupplierId 时,我收到此错误:
{"Multiplicity constraint violated. The role 'ProductSupplierForContract_Product_Target' of the relationship 'ContractCare.Models.ProductSupplierForContract_Product' has multiplicity 1 or 0..1."}
我不明白为什么,因为不是这样我就可以拥有:
PSFC 1
PK 1
ProductId 1
SupplierId 1
PSFC 2
PK 2
ProductId 1
SupplierId 2
为什么我(如上文链接 post 中所述)在产品上具有多对多关系 PSFC?
感谢您的任何反馈!
亲切的问候。
关注link可能会在这个问题上对您有更多帮助!
- Multiplicity constraint violated. The role '...' of the relationship '...' has multiplicity 1 or 0..1
扩展 link,Aparna Gadgil provided, one of the comments posted by Arturo Hernandez 解决了我遇到的错误,所以我将标记为已解决,因为我最初的问题是错误。
引用:
The problem is that detachedChild.parent should be assigned
attachedParent.
foreach(var detachedEntity in detachedEntities)
{
attachedEntity.ChildEntities.Add(detachedEntity);
detachedEntity.ParentEntity = attachedEntity;
}
在我的例子中,我必须将父实体(我们正在更新的产品)附加到 ProductSupplierForContract.Product。像答案中建议的那样创建新实体对我来说已经是这样了,附加它等其他事情并没有为我修复它。
因为我没有 post 任何关于我如何执行此操作的代码,所以能够帮助我并不是很有帮助,我肯定会在未来尝试更具体:) .
谨致问候,谢谢!
我有一些我不明白的地方。 This 和我的情况类似。 我有 BaseEntity、Product、Supplier、Contract 和 ProductSupplierForContract 实体,它们都继承自 BaseEntity。
基础实体:
public class BaseEntity
{
public int ID { get; set; }
// other properties that are not entities by themself
}
产品实体:
[Required]
public ICollection<Supplier> Suppliers { get; set; }
[Required]
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
public ICollection<Contract> Contracts { get; set; }
供应商实体:
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
public ICollection<Product> Products { get; set; }
ProductSupplierForContract 实体:
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; }
合同实体:
[Required]
public ICollection<Product> Products { get; set; }
public ICollection<Supplier> Suppliers { get; set; }
public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
以下场景必须是可能的并被允许: 1 个合同可以包含来自不同供应商的 1 个产品的多个实例。 因此,我创建了 ProductSupplierForContract (PSFC) 实体,它将保持 1product-1supplier-1productnumber (value) -1contract .
的这种关系当我编辑 1 个已有 1 个 PSFC 实例的现有产品,并添加另一个 PSFC 实例持有另一个 PK、ProductId 和 SupplierId 时,我收到此错误:
{"Multiplicity constraint violated. The role 'ProductSupplierForContract_Product_Target' of the relationship 'ContractCare.Models.ProductSupplierForContract_Product' has multiplicity 1 or 0..1."}
我不明白为什么,因为不是这样我就可以拥有:
PSFC 1
PK 1
ProductId 1
SupplierId 1
PSFC 2
PK 2
ProductId 1
SupplierId 2
为什么我(如上文链接 post 中所述)在产品上具有多对多关系 PSFC?
感谢您的任何反馈! 亲切的问候。
关注link可能会在这个问题上对您有更多帮助!
- Multiplicity constraint violated. The role '...' of the relationship '...' has multiplicity 1 or 0..1
扩展 link,Aparna Gadgil provided, one of the comments posted by Arturo Hernandez 解决了我遇到的错误,所以我将标记为已解决,因为我最初的问题是错误。
引用:
The problem is that detachedChild.parent should be assigned attachedParent.
foreach(var detachedEntity in detachedEntities)
{
attachedEntity.ChildEntities.Add(detachedEntity);
detachedEntity.ParentEntity = attachedEntity;
}
在我的例子中,我必须将父实体(我们正在更新的产品)附加到 ProductSupplierForContract.Product。像答案中建议的那样创建新实体对我来说已经是这样了,附加它等其他事情并没有为我修复它。
因为我没有 post 任何关于我如何执行此操作的代码,所以能够帮助我并不是很有帮助,我肯定会在未来尝试更具体:) .
谨致问候,谢谢!