简体   繁体   English

发生参照完整性约束违规

[英]Referential integrity constraint violation occurred

I am getting the following error when calling context.Set<Person>().Attach(person); 调用context.Set<Person>().Attach(person);时出现以下错误context.Set<Person>().Attach(person); on EF 5.0 在EF 5.0上

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship. 发生参照完整性约束冲突:定义参照约束的属性值在关系中的主体对象和从属对象之间不一致。

Is there some way to figure out which exact property is generating this error? 有什么办法可以找出哪个确切的属性正在产生此错误?

I know what the cause is, that a foreign key which is directly a property of an object is different from the related object's primary key, so for example: 我知道是什么原因,直接是对象属性的外键与相关对象的主键不同,例如:

Person --> Address
ID         PersonID
           Person

and Address.PersonID isn't the same as Address.Person.PersonID , but no matter where I look at my real life object I am not getting which property is causing this. Address.PersonIDAddress.Person.PersonID ,但是无论我在现实生活中的位置如何,都不会知道是哪个属性引起的。

So is there some way to get to the root of the problem, maybe stepping through the ObjectStateManager or some ChangeTracking routine? 那么,是否有某种方法可以解决问题的根源,例如逐步执行ObjectStateManager或某些ChangeTracking例程?

I have already written a t4 template file (to autogenerate it based on my entitymodel) that checks every ForeignKey/PrimaryKey, and I just cannot find a difference. 我已经编写了一个t4模板文件(基于我的entitymodel自动生成它),该文件检查每个ForeignKey / PrimaryKey,但我只是找不到任何区别。

Multi ForeignKey ruined my day... 多ForeignKey毁了我的一天...

So to complete my sample above it was like; 因此,要完成我上面的样本就好了;

Person --> Address
ID         PersonID
ID2        PersonID2
           Person

And I didnt check the ID2 of my objects, which was loaded incorrectly. 而且我没有检查我的对象的ID2,它的加载不正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发生了参照完整性约束违规 - A referential integrity constraint violation occurred 错误“发生了参照完整性约束冲突” - Error “A referential integrity constraint violation occurred” 发生参照完整性约束冲突 Nullable FK - A referential integrity constraint violation occurred Nullable FK 发生参照完整性约束冲突。 更新EF时 - A referential integrity constraint violation occurred. When Updating EF 向EF添加新实体时出错:发生了参照完整性约束违规 - Error when adding new entity to EF : A referential integrity constraint violation occurred 尝试在Entity Framework中进行更新时出现错误“发生了引用完整性约束冲突” - Error “A referential integrity constraint violation occurred” when trying update in Entity Framework 实体框架迁移-违反参照完整性约束 - Entity Framework Migrations - A referential integrity constraint violation EF 6 Code First,使用包含在导航属性上的外键ID更改会导致“发生引用完整性约束违规”错误 - EF 6 Code First, changing a Foreign Key Id with an Include on navigation property causes “A referential integrity constraint violation occurred” error 保存包含列表的对象会产生“违反参照完整性约束” - Saving an Object that contains a list gives “referential integrity constraint violation” 尝试将FK设置为​​null时,引用完整性约束违规 - Referential Integrity Constraint violation when attempting to set a FK to null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM