0

我正在使用EF5WinForms 并有一个发票实体列表。我只想更新发票的一个属性,但是如果我不更新导航属性,我会在 SaveChanges 上收到错误消息。

为什么?

 var unpostedInvoices =   db.Invoices.Where(  o => (o.PostStatus ==
 (int)PostStatus.UnPosted) ).ToList();

    foreach (invoice inv in unpostedInvoices) 
    {
        inv.PostStatus = (int)PostStatus.Posted)
        inv.User = inv.User;    // if this line is not put in I get an error:  "The user field is required" 
    } 
    db.SaveChanges
4

1 回答 1

1

将 Include 方法调用添加到您的初始查询:

var unpostedInvoices =   db.Invoices.Include("User").Where((o => (o.PostStatus == (int)PostStatus.UnPosted) ).ToList();
于 2013-10-21T06:56:45.587 回答