[英]How to set null foreign key in Entity Framework code-first
我有一個訂單<客戶模型。
我該如何設置? 當我嘗試插入一個客戶ID為空的用戶時,它告訴我不能為空。
該模型如下:
public class Order
{
public long OrderID { get; set; }
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}
public partial class Customer
{
public int CustomerID { get; set; }
}
在DbContext
:
modelBuilder.Entity<Customer>()
.HasMany(e => e.Orders)
.WithRequired(e => e.Customer)
.WillCascadeOnDelete(false);
您是否嘗試過更換線
.WithRequired(e => e.Customer)
與
.WithOptional(e => e.Customer)
?
我將CustomerID屬性設置為可空:public int? 客戶ID {get; 組; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.