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

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

问题描述

这是我的实体:

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; }
}



我第一次值插入数据库一切正常,但如果我更改了数据库的任何值,并尝试插入或更新记录我得到这个错误:

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:

操作失败:关系不能改变,因为
的一个或多个外键的属性是不可为空。当
变化有关系提出的有关外键属性为
设置为空值。如果外键不支持空值,$ B $巴新的关系必须定义,外键属性必须是
指派另一非空值,或者无关的对象必须是
删除

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();
}



我搜索计算器和发现的这个。但我不明白我要做

推荐答案

我觉得问题就出在这个代码块:

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 对象货币之前。现在这个对象仍然是在数据库中,但没有父母。这样的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 对象总是依赖于他们的货币父母,你可能会考虑这样的一个标识关系。我已经解释了如何在实现与EF4 的识别与关系创建的EntityFramework他们4模型优先。我还没有与代码第一次没有制定,但该方法应该是相似的。

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天全站免登陆