The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable

c# entity-framework

Question

These are my entities:

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:

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.

This is my insert code:

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 searched StackOverflow and found this. But I don't understand what I have to do.

1
5
5/23/2017 12:16:04 PM

Accepted Answer

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

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.

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.

10
5/23/2017 12:16:04 PM

Popular Answer

Had same error when saving changes on the context, and didn't made any changes to entities or relationships.

Error was because some entities had GetHashCode() method ovveridden.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow