title: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable
body: <p>These are my entities:</p>
  
  <pre><code>public class Currency : Exchange
  {
       public List&lt;CurrencyPrice&gt; CurrencyPrice { get; set; }
  }
  
  public class CurrencyPrice : Price
  {
      public int CurrencyId { get; set; }
      [ForeignKey("CurrencyId")]
      public Currency Currency { get; set; }
  }
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>This is my insert code:</p>
  
  <pre><code>var currency =
      UnitOfWork.Exchange.Get(x =&gt; x is Currency &amp;&amp; x.ExchangeType.Id == exchangeTypeId) as Currency;
  if (currency != null)
  {
      CurrencyPrice lastPriceValue = UnitOfWork.CurrencyPrice.Last(x =&gt; x.CurrencyId == currency.Id);
      if (lastPriceValue == null || lastPriceValue.Value != price)
      {
          currency.CurrencyPrice = new List&lt;CurrencyPrice&gt;
              {
                  new CurrencyPrice {Id = Guid.NewGuid().ToString(), EntryDate = DateTime.Now, Value = price,CurrencyId = currency.Id,Currency = currency}
              };
      }
      else
      {
          lastPriceValue.EntryDate = DateTime.Now;
      }
  
      UnitOfWork.Commit();
  }
  </code></pre>
  
  <p>I searched StackOverflow and found <a href="https://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign-key-pro">this</a>. But I don't understand what I have to do.</p>