![](/img/trans.png)
[英]EF 6.1.1 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable
[英]EF SaveChanges method throws: The relationship could not be changed because one or more of the foreign-key properties is non-nullable
當我從Winforms應用程序保存訂單時,它可以正常運行。 但是,當我嘗試添加第二個時,SaveChanges()方法將引發異常:
操作失敗:由於一個或多個外鍵屬性不可為空,因此無法更改該關系。 對關系進行更改時,相關的外鍵屬性將設置為空值。 如果外鍵不支持空值,則必須定義新的關系,必須為外鍵屬性分配另一個非空值,或者必須刪除不相關的對象。
我有一個表“ Orders”和一個具有外鍵OrderID的“ Orderlines”。
有誰知道為什么會這樣嗎?
我知道我的上下文處理不是理想的,因此,如果這是問題所在,那么關於如何更改此問題的任何建議將非常有必要!
public class Orders
{
private readonly DataContext _context;
public Orders()
{
_context = EntityContext.Context;
}
public int Save(Order order)
{
if (order.ID > 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();
}
}
編輯1在異常時間添加了Order類和訂單的屏幕截圖
我已將更新完整性設置為CASCADE。
訂單行是一個關系表。
public partial class Order
{
public Order()
{
this.Orderlines = new HashSet<Orderline>();
}
public int ID { get; set; }
public System.DateTime CreatedDate { get; set; }
public int PaymentType { get; set; }
public Nullable<int> SettlementID { get; set; }
public Nullable<decimal> CashPayment { get; set; }
public Nullable<decimal> BankPayment { get; set; }
public Nullable<int> UserID { get; set; }
public virtual ICollection<Orderline> Orderlines { get; set; }
public virtual Settlement Settlement { get; set; }
}
編輯2-局部類
我有部分類擴展了order和orderline對象:
public partial class Order
{
public decimal Total
{
get { return Orderlines.Sum(x => x.Quantity*x.Price); }
}
public decimal TotalExMva
{
get { return Orderlines.Sum(x => (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;
}
}
}
編輯3-訂單行和產品類別
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<Orderline>();
}
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<Orderline> Orderlines { get; set; }
}
此博客對此進行了很好的描述: http : //blogs.msdn.com/b/dsimmons/archive/2010/01/31/deleting-foreign-key-relationships-in-ef4.aspx
並且是一個稱為識別關系的問題: http : //msdn.microsoft.com/zh-cn/library/vstudio/ee373856(v=vs.100).aspx
TRex
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.