title: EF SaveChanges method throws: The relationship could not be changed because one or more of the foreign-key properties is non-nullable
body: <p>When i save an order from my winforms app it works ok. But when i try to add a second one the SaveChanges() method throws an exception:</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>I have one table "Orders" and one "Orderlines" that has a foreignkey OrderID.</p>
  
  <p>Does anyone know why this happens?</p>
  
  <p>I know that my context handling is not ideal, so if that is the problem, any suggestions on how to change this would be much obliged!</p>
  
  <pre><code>public class Orders
  {
      private readonly DataContext _context;
  
      public Orders()
      {
          _context = EntityContext.Context;
      }
  
      public int Save(Order order)
      {
          if (order.ID &gt; 0)
              _context.Entry(order).State = EntityState.Modified;
          else
          {
              _context.Orders.Add(order);
          } 
              _context.SaveChanges();
              return order.ID;
      }
  }
  public class EntityContext : IDisposable
  {
      private static DataContext _context;
      public static DataContext Context
      {
          get
          {
              if (_context != null)
                  return _context;
              return _context = new DataContext();
          }
      }
  
      public void Dispose()
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p><strong>EDIT 1</strong> Added Order class and screencapture of order on exceptiontime</p>
  
  <p>I have set update integrity to CASCADE.</p>
  
  <p>Orderlines is a relationship table.</p>
  
  <pre><code>public partial class Order
  {
      public Order()
      {
          this.Orderlines = new HashSet&lt;Orderline&gt;();
      }
  
      public int ID { get; set; }
      public System.DateTime CreatedDate { get; set; }
      public int PaymentType { get; set; }
      public Nullable&lt;int&gt; SettlementID { get; set; }
      public Nullable&lt;decimal&gt; CashPayment { get; set; }
      public Nullable&lt;decimal&gt; BankPayment { get; set; }
      public Nullable&lt;int&gt; UserID { get; set; }
  
      public virtual ICollection&lt;Orderline&gt; Orderlines { get; set; }
      public virtual Settlement Settlement { get; set; }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/xt3xg.png" alt="Object order on exception"></p>
  
  <p><strong>EDIT 2</strong> - Partial classes</p>
  
  <p>I have partial classes extending the order and orderline objects:</p>
  
  <pre><code>public partial class Order 
  {
      public decimal Total
      {
          get { return Orderlines.Sum(x =&gt; x.Quantity*x.Price); }
      }
      public decimal TotalExMva
      {
          get { return Orderlines.Sum(x =&gt; (x.Quantity*x.Price)/(x.Vat + 1)); }
      }
      public decimal Mva
      {
          get { return Total - TotalExMva; }
      }
  
  }
  
  public partial class Orderline
  {
      public decimal Total
      {
          get { return (Price*Quantity); }         
      }
  
      public string ProductName
      {
  
          get
          {
              var currentProductId = ProductID;
              return new Products().Get(currentProductId).Name;
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT 3</strong> - orderline and product classes</p>
  
  <pre><code>public partial class Orderline
  {
      public int ID { get; set; }
      public int ProductID { get; set; }
      public int Quantity { get; set; }
      public decimal Price { get; set; }
      public decimal Vat { get; set; }
      public int OrderID { get; set; }
  
      public virtual Order Order { get; set; }
      public virtual Product Product { get; set; }
  }
  public partial class Product
  {
      public Product()
      {
          this.Orderlines = new HashSet&lt;Orderline&gt;();
      }
  
      public int ID { get; set; }
      public string Name { get; set; }
      public string GTIN { get; set; }
      public decimal Price { get; set; }
      public decimal Vat { get; set; }
  
      public virtual ICollection&lt;Orderline&gt; Orderlines { get; set; }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/me0ED.png" alt="Orderlines -&gt; Orderline object at exception"></p>