0
我只是想利用實體框架6.我想這個更新客戶和他的命令之一,但它正在更新的客戶不僅沒有他的命令:如何更新導航集合屬性
Customer customer = db.Cutomers.Include(x => x.Orders).Where(y => y.Id ==1234).SingleOrDefault();
customer.Name = "Joe"; // changing the customer name
customer.Orders[0].OrderDesc = "NewDesc"; // changing one of the orders
customer.Orders[0].OrderDate = DateTime.Now;
using (var db = new Context())
{
db.Cutomers.Attach(customer);
db.Entry(customer).Collection(e => e.Orders).Load();
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
}
好的。我明白,在我做出更改之前,我應該從數據庫加載數據,但答案的第二部分對我而言並不清楚。我如何「將訂單本身附加到收藏以便更新。」 – user3237706
@ user3237706很難說出你是如何加載你的實體,除非你發佈'GetCustomerFromDB'函數的代碼。查看我的編輯,瞭解我的意思。 – SOfanatic
我更新了代碼以顯示我如何獲得客戶。我嘗試了你的建議,但我想我仍然錯過了一些東西。 – user3237706