参照完整性约束冲突发生 [英] A referential integrity constraint violation occurred

查看:551
本文介绍了参照完整性约束冲突发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试uodate现有实体。

I try to uodate an existing entity.

我有以下的code:

public MamConfiguration_V1 Save(MamConfiguration_V1 item)
{
    mMaMDBEntities.MamConfiguration_V1.Attach(item);
    mMaMDBEntities.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Modified);
    mMaMDBEntities.SaveChanges();
    return item;
}

连接方法抛出异常:

一个参照完整性约束冲突发生了:定义引用约束的属性值是不是本金和依赖对象的关系之间是一致的。

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.

我该如何解决这个问题?

How can I fix this?

推荐答案

什么是项目对象的定义是什么?看来,在一些收藏品的设置与其他实体的realionship的存在着一些类型的冲突。你可以尝试清除所有集合,看看问题是否仍然存在,但在这种情况下,你失去了外键的分配。但也许它可以帮助你找到问题。

What is the definition of the item object? It seems that in some of its collections that set the realionship with other entities exist some type of conflict. You could try to clear all the collections to see if the problem persists, but in this case you lost the foreign key assignment. But perhaps it could help you to locate the problem.

这可能是一个提示。当我尝试将现有实体重视的背景下,我用它来执行以下操作:

This could be a tip. When I try to attach an existing entity to the context, I use to do the following:

mMaMDBEntities.Entry<MamConfiguration>(item).State = System.Data.EntityState.Modified;

您可以添加使用System.Data这以避免将它写所有的时间所需要的。

You can add the using of System.Data to avoid the needed to write it all the time.

这个附加在所需的状态的实体,在这种情况下,修改和跟踪的变化。这是一条线,而不是两个。

This attach the entity in the state that you want, modified in this case and track the changes. This is one line instead of two.

这篇关于参照完整性约束冲突发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆