[英]EF6: Multiple added entities may have the same primary key
I've inherited a database and I need to insert data using EF6. 我继承了一个数据库,需要使用EF6插入数据。 I get the error:
我收到错误:
DbUpdateException: Unable to determine the principal end of the 'POSModel.FK_KitMemberTaxRaw_KitMemberSaleReturnRaw_KitMemberSaleReturnRowId' relationship. Multiple added entities may have the same primary key.
I deserialize XML to the POCO objects using DataContractSerializer. 我使用DataContractSerializer将XML反序列化为POCO对象。 I'm using the object references from the xml document's structure to define the relationships.
我正在使用xml文档结构中的对象引用来定义关系。 The POCO objects are generated using a t4 script provided from the NuGet package (which does not work with either deserializer well at all!)
POCO对象是使用NuGet软件包提供的t4脚本生成的(根本无法与任何解串器配合使用!)
I've decorated KitMemberTaxRaw like so: 我已经这样装饰KitMemberTaxRaw:
[ForeignKey("KitMemberSaleReturnRaw")]
public virtual KitMemberSaleReturnRaw KitMemberSaleReturnRaw { get; set; }
[ForeignKey("KitMemberKitMemberSaleReturnRaw")]
public virtual KitMemberKitMemberSaleReturnRaw KitMemberKitMemberSaleReturnRaw { get; set; }
The KitMemberTaxRaw table may be joined to either table KitMemberKitMemberSaleReturnRaw or KitMemberSaleReturnRaw (but not both). KitMemberTaxRaw表可以连接到表KitMemberKitMemberSaleReturnRaw或KitMemberSaleReturnRaw(但不能同时)。
How does EF determine 'the principal end of the relationship'? EF如何确定“关系的主要目的”?
The issue turned out to be EF6 was not able to automatically understand a table with links to a parent and an optional grand parent. 事实证明,此问题是由于EF6无法自动理解包含指向父级和可选的父级父级的链接的表。 The navigation properties generated by the Microsoft provided template were correct but insufficient.
Microsoft提供的模板生成的导航属性正确但不足。
To cure the issue I manually created temporary primary keys for the relationship it did not understand. 为了解决该问题,我为它不了解的关系手动创建了临时主键。
Note: The DataContractSerializer class I used created the POCO objects creates an array for the instantiated navigation properties. 注意:我使用的DataContractSerializer类创建了POCO对象,为实例化的导航属性创建了一个数组。 I had to change the template to generate IList<> properties instead of ICollection<>.
我必须更改模板以生成IList <>属性而不是ICollection <>。 At run time there were errors because the array could not be dynamically resized.
在运行时出现错误,因为无法动态调整数组大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.