简体   繁体   English

实体框架参照完整性问题

[英]Entity Framework referential integrity issue

The problem:问题:

I have the code which is adding/updating properties with EF.我有使用 EF 添加/更新属性的代码。 Previously it was calling after modifying each property SaveChanges() and in that case it was working.以前它在修改每个属性 SaveChanges() 后调用,在这种情况下它正在工作。 What I have done.我做了什么。 I have modified the code to have a batch (10 items at this time) and after inserting number of entries call SaveChanges().我已将代码修改为批处理(此时为 10 个项目),并在插入条目数后调用 SaveChanges()。 But sometimes it starts to throw such an exception但有时它开始抛出这样的异常

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.发生参照完整性约束冲突:定义参照约束的属性值在关系中的主体和从属对象之间不一致。

Issue I found:我发现的问题:

The issue I found when I tried to modify 14 entries.我在尝试修改 14 个条目时发现的问题。 First batch of 10 entries it is doing good.第一批 10 个条目做得很好。 Than it is adding 11th entry and when tries to add 12th it was throwing this exception.比它添加第 11 个条目,当尝试添加第 12 个条目时,它抛出了这个异常。 I checked the code and found part which is causing exception.我检查了代码并找到了导致异常的部分。

property.CountryId = importProperty.CountryId;

It is setting CountryId to 123, but navigation property Country here has Id set to 54 (I am not changing this, just got version from database).它将 CountryId 设置为 123,但此处的导航属性 Country 将 Id 设置为 54(我没有更改此设置,只是从数据库中获取了版本)。 Setting navigation property to null manually is throwing such an error手动将导航属性设置为 null 会引发这样的错误

The relationship could not be changed because one or more of the foreign-key properties is non-nullable.由于一个或多个外键属性不可为空,因此无法更改关系。 When a change is made to a relationship, the related foreign-key property is set to a null value.当对关系进行更改时,相关的外键属性将设置为空值。 If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

Question:题:

What should I do?我该怎么办? Why it is working when I'm calling SaveChanges after each entry and not working in this case?为什么当我在每次输入后调用 SaveChanges 并且在这种情况下不起作用时它起作用? I found questions in Stackoverflow about this problem, but no one solves my issue.我在 Stackoverflow 中发现了有关此问题的问题,但没有人解决我的问题。

Some code:一些代码:

public void InsertOrUpdateEntity(TEntity entity)
{
    if (entity.ObjectState == ObjectState.Added)
    {
           dbSet.Add(entity);
    }
    else
    {
           dbSet.Attach(entity);
           context.ApplyStateChanges();
    }
}

Error occured on the line dbSet.Attach(entityGraph);错误发生在dbSet.Attach(entityGraph); with a property mentioned above.具有上述属性。

This might be the answer.这可能就是答案。

Assign the CountryID to 123, either load the Country entity for 123 or create a new instance of the Country entity and assign it's ID to 123, Attach the country entity to the entity context (ie dbset) and assign it to the Country property.将 CountryID 分配给 123,或者为 123 加载 Country 实体,或者创建 Country 实体的新实例并将其 ID 分配给 123,将国家实体附加到实体上下文(即 dbset)并将其分配给 Country 属性。

The problem was that 11th and 12th rows were same entity in database.问题是第 11 行和第 12 行是数据库中的相同实体。

Entity Framework changed Country Id from 101 to 54 at 11th record and than to 123 at 12th record (and at the end EF finished with CountryID = 123 and Country.Id = 54 in case when at the beginning both of them were 101!!).实体框架在第 11 条记录时将 Country Id 从 101 更改为 54,然后在第 12 条记录时更改为 123(并且最终 EF 以 CountryID = 123 和 Country.Id = 54 结束,以防一开始它们都是 101 !!) .

Problem was fixed with condition to not allow multiple changes of same entity.问题已修复,条件是不允许对同一实体进行多次更改。

就我而言,我通过在 edmx 模型的更新向导中选中“在模型中包含外键列”来解决。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM