EF 5,更新对象给出"参照完整性约束冲突发生" [英] EF 5, update object gives "A referential integrity constraint violation occurred"

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

问题描述

在我的模型我有一大堆的领域对象。现在,我尝试更新的用户对象时,有一个问题。用户有一个ForeignKey的关系中的作用对象。当我没有量变到质变的ForeignKey的值(FkRoleId)更新的用户对象这一切工作正常。但是,当我改变我想更新当前用户的角色我得到的错误:


  

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


下面是我如何更新我的用户对象:

 公共无效更新(用户用户)
{
    使用(VAR上下文=新DBEntities())
    {
    context.Entry(用户).STATE = System.Data.EntityState.Modified;
    context.SaveChanges();
    }
}

我怎么能没有得到此异常更新我的用户对象?
必须有chaning的ForeignKey的值,为用户mappen到另一个角色的方法。

下面是在这种情况下,我的域名类别:

 公共部分类用户
{
公共用户()
{
    this.Advertisers =新的HashSet<&广告商GT();
    this.Cases =新的HashSet<箱及GT;();
    this.Materials =新的HashSet<材料>();
}公众诠释PKID {搞定;组; }
公众诠释FkRoleId {搞定;组; }
公共字符串名称{;组; }
公共字符串电子邮件{获得;组; }
公共字符串密码{搞定;组; }
公众的System.DateTime创建{搞定;组; }
公共BOOL活跃{搞定;组; }公共虚拟的ICollection<&广告商GT;广告商{搞定;组; }
公共虚拟的ICollection<外壳>案件{搞定;组; }
公共虚拟的ICollection<材料>材料{搞定;组; }
公共虚拟角色角色{搞定;组; }
}
公共部分类角色
{
公共角色()
{
    this.Users =新的HashSet<使用者>();
}公众诠释PKID {搞定;组; }
公共字符串名称{;组; }
公共字符串描述{搞定;组; }公共虚拟的ICollection<使用者>用户{搞定;组; }
}


解决方案

要引用您的评论:


  

不过,如果我设置的导航属性user.Role为null它的工作原理
  正好。但是,这是工作的推荐方法?


是,在这种情况下,它是

您必须保持你进入一个新的上下文有一种超然的心态对象用户(包括参照 user.Role )。通过将状态设置为修改您可以将对象用户与关联起来 user.Role 这个新环境。

由于您使用的是外键关联 - 这意味着,外键是由模型类的属性 FkRoleId psented重新$ P $ - 关系进行说明有两种方式:通过导航属性 user.Role 和标量属性 user.FkRoleId 。如果这些都没有一致的 - 即 user.Role.PkId = user.FkRoleId - EF不知道哪一个描述正确的关系,并引发你有例外<。 / p>

如果您设置 user.Role EF会考虑 user.FkRoleId 单独作为描述用户和作用,这是造成异常被删除的模糊性之间的关系属性。

In my model I've got a bunch of domain objects. Now I'm having a problem when trying to update a User-object. The User has a foreignkey relation to the Role object. When I update the User-object without changeing the foreignkey value (FkRoleId) it all works fine. But when I change the role for the current user I want to update I get the error:

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.

Here's how I update my user-object:

public void Update(User user)
{
    using (var context = new DBEntities())
    { 
    context.Entry(user).State = System.Data.EntityState.Modified;
    context.SaveChanges();
    }
}

How can I update my user-object without getting this exception? There must be a way for chaning the foreignkey value for mappen the user to another role.

Here's my domain classes in this case:

public partial class User
{
public User()
{
    this.Advertisers = new HashSet<Advertiser>();
    this.Cases = new HashSet<Case>();
    this.Materials = new HashSet<Material>();
}

public int PkId { get; set; }
public int FkRoleId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public System.DateTime Created { get; set; }
public bool Active { get; set; }

public virtual ICollection<Advertiser> Advertisers { get; set; }
public virtual ICollection<Case> Cases { get; set; }
public virtual ICollection<Material> Materials { get; set; }
public virtual Role Role { get; set; }
}


public partial class Role
{
public Role()
{
    this.Users = new HashSet<User>();
}

public int PkId { get; set; }
public string Name { get; set; }
public string Description { get; set; }

public virtual ICollection<User> Users { get; set; }
}

解决方案

To quote your comment:

However; if I set the navigation property user.Role to null it works just fine. But, is this the recommended way of working?

Yes, in this case it is.

You must keep in mind that you enter a new context with a detached object user (including the reference to user.Role). By setting the state to Modified you attach the object user together with the related user.Role to this new context.

Because your are using a foreign key association - that means that the foreign key is represented by a property FkRoleId in the model class - the relationship is described in two ways: By the navigation property user.Role and by the scalar property user.FkRoleId. If these are not consistent - i.e. user.Role.PkId != user.FkRoleId - EF doesn't know which one describes the correct relationship and throws the exception you have.

If you set user.Role to null EF will consider the user.FkRoleId alone as the property that describes the relationship between user and role and the ambiguity that was causing the exception is removed.

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

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