实体框架:参照完整性约束违反了多对多的关系 [英] Entity Framework: A referential integrity constraint violation on many to many relationship

查看:450
本文介绍了实体框架:参照完整性约束违反了多对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一堆INPROC缓存和实体框架的应用。当我想写一个更新的实体我重新安装缓存副本。我跟踪我已经把它贴在上下文的生命周期,所以我不尝试将他们两次一切。

Hey I have an application with a bunch of inproc caching and entity framework. When I want to write an update to an entity I reattach the cached copy. I track all things I've attached in the life cycle of the context so I don't try to attach them twice.

我也发生在连接(十分错误很少在大多数情况下,这工作得很好,是非常快),它说以下内容:

I have an error occurring on attach (very rarely in most cases this works fine and is really fast) which says the following:

发生参照完整性约束冲突:属性$ b定义参照约束委托人和依赖对象的关系之间并不一致
$ b值。

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.

我已经在拍摄看起来正常的实体的真正细细品味。我认为这个问题是由于的修正运行时外键的安装/拆卸。

I've taken a really careful look at the entity which looks normal. I think this issue is due to the attachment/detachment of a foreign key when fixups runs.

有没有好的办法让这个错误任何更多的信息,也可以出现比其他原因,该实体在EF wasnt期待的状态?

Is there a good way to get any more info on this error or can it occur for reasons other than that the entity was in a state which EF wasnt expecting?

编辑:
DB图(注意:我使用codefirst我只是用EDMX工具,使图中,我也切了一堆常规属性关闭型号为简单起见)

DB Diagram (note i'm using codefirst I just used the EDMX tool to make the diagram, I've also chopped a bunch of regular properties off the model for simplicity)

推荐答案

可能发生在人之间的一个一对多关系错误位置你显然有除了许多一对多的关系模型。例如,下面的代码会抛出异常:使用(VAR上下文=新MyContext())

The error could occur for the one-to-many relationship between Person and Location you apparently have in your model in addition to the many-to-many relationship. For example the following code would throw the exception:

using (var context = new MyContext())
{
    var person = new Person
    {
        CurrentLocationId = 1,
        CurrentLocation = new Location { Id = 2 }
    };
    context.People.Attach(person); // Exception
}



定义引用约束的属性值都是国外关键属性值 CurrentLocationId 和主键值 CurrentLocation.Id 。如果这些值不同的异常。 (有 CurrentLocation 虽然是允许的。)

"The property values that define the referential constraints" are the foreign key property value CurrentLocationId and the primary key value CurrentLocation.Id. If those values are different the exception is thrown. (Having CurrentLocation as null though is allowed.)

在我看来,这异常只能被抛出外键关联,因为只有这种类型的关联,你必须在你的模型在所有的定义引用约束属性。它不能被抛出独立关联。由于每个许多一对多的关系是一个独立的协会(在模型中没有外键属性)我的猜测是错误是不相关的许多一对多的关系,而是一对多。

In my opinion this exception can only be thrown for foreign key associations because only for this type of association you have properties that define referential constraints at all in your model. It cannot be thrown for independent associations. Since every many-to-many relationship is an independent association (no foreign key property in the model) my guess is that the error is not related to your many-to-many relationship, but to the one-to-many.

这篇关于实体框架:参照完整性约束违反了多对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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