操作失败:无法更改关系,因为一个或多个外键属性不可为空 [英] The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable

查看:754
本文介绍了操作失败:无法更改关系,因为一个或多个外键属性不可为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我的实体:

public class Currency : Exchange
{
     public List<CurrencyPrice> CurrencyPrice { get; set; }
}

public class CurrencyPrice : Price
{
    public int CurrencyId { get; set; }
    [ForeignKey("CurrencyId")]
    public Currency Currency { get; set; }
}

第一次在DB中插入一个值一切都可以,但如果我更改数据库中的任何值,并尝试插入或更新记录我得到这个错误:

The first time I insert a value into the DB everything is OK but if I change any value in the DB and try to insert or update the record I get this error:


操作失败:关系不能被更改,因为
一个或多个外键属性不可为空。当对关系进行
更改时,相关的外键属性
设置为空值。如果外键不支持空值,则必须定义
a新关系,外键属性必须为
分配另一个非空值,否则不相关对象必须为
删除

The operation failed: 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.

这是我的插入代码:

var currency =
    UnitOfWork.Exchange.Get(x => x is Currency && x.ExchangeType.Id == exchangeTypeId) as Currency;
if (currency != null)
{
    CurrencyPrice lastPriceValue = UnitOfWork.CurrencyPrice.Last(x => x.CurrencyId == currency.Id);
    if (lastPriceValue == null || lastPriceValue.Value != price)
    {
        currency.CurrencyPrice = new List<CurrencyPrice>
            {
                new CurrencyPrice {Id = Guid.NewGuid().ToString(), EntryDate = DateTime.Now, Value = price,CurrencyId = currency.Id,Currency = currency}
            };
    }
    else
    {
        lastPriceValue.EntryDate = DateTime.Now;
    }

    UnitOfWork.Commit();
}

我搜索到StackOverflow并发现这个。但是我不明白我要做什么。

I searched StackOverflow and found this. But I don't understand what I have to do.

推荐答案

我认为问题在于这个代码块:

I think the problem lies in this code block:

currency.CurrencyPrice = new List<CurrencyPrice>
{
    new CurrencyPrice {Id = Guid.NewGuid().ToString(), EntryDate = DateTime.Now, Value = price,CurrencyId = currency.Id,Currency = currency}
};

在此您创建一个新的 CurrencyPrice 并分配它到一个货币,从而孤立分配给这个货币的 CurrencyPrice code>之前。此对象现在仍在数据库中,但没有父级。因此EntityFramework想要将这个 CurrencyPrice 上的父项的ID设置为 NULL ,数据库阻止导致您引用的错误。我已经在这个答案

Here you create a new CurrencyPrice and assign it to a Currency, thereby orphaning the CurrencyPrice object that was assigned to this Currency before. This object is now still in the database, but without a parent. Thus EntityFramework wants to set the parent's ID on this CurrencyPrice to NULL which the database prevents resulting in the error you quoted. I have posted a more detailed explanation of this problem in this answer.

为了防止这种情况,删除旧的 CurrencyPrice 在添加新数据库之前从数据库中删除。或者,由于似乎 CurrencyPrice 对象始终取决于他们的货币父,您可以考虑使其成为识别关系。我已经解释了如何使用EntityFramework创建它们4模型首先在实现与EF4的识别关系 。我还没有使用Code First,但是这个方法应该是相似的。

To prevent this, remove the old CurrencyPrice from your database before adding the new one. Alternatively, since it seems that CurrencyPrice objects always depend on their Currency parent, you might consider making this an identifying relationship. I have explained how to create them with EntityFramework 4 Model First at Implementing identifying relationships with EF4. I have not worked with Code First yet, but the approach should be similar.

这篇关于操作失败:无法更改关系,因为一个或多个外键属性不可为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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