对于这个愚蠢的问题感到抱歉,但这是我对WPF和Entity Framework的第一种方法。使用EntityFramework更新WPF ObservableCollection中的主 - 细部项目
让我们来解释一下我的情况......
我有一个主类(客户)和细节类(订单),无论是在我的EF上下文。 我使用LINQ-to-EF查询(包括Orders)加载我的主类,并将IEnumerable结果集放入ObservableCollection中,该列被用作ListBox的源。
然后我有一个DataGrid在哪里加载订单。 现在,由于主项目位于ObservableCollection中,因此我可以添加,删除和更新项目,并自动反映到我的ListBox。 问题是我需要向客户添加订单。
有没有办法只更新ObservableCollection中的一个项目,而无需重新加载上下文中的所有项目?
这是我的代码(简化)
// Loading Customers
EntitiesContext context = new EntitiesContext();
IEnumerable<Customer> customers = from c in context.Customer.Include("Orders")
select c;
ObservableCollection<Customer> oc = new ObservableCollection<Customer>(customers);
// Binding ListBox
listCustomers.ItemsSource = oc;
...
// Retrieving a Customer (1st for instance...)
Customer c = (listCustomers.Items[0] as Customer);
Order o = new Order {Customer = c.ID, Item = "xxx"};
context.AddToOrders(o);
// How can I update the ObservableCollection?
在此先感谢,问题的
曼努埃尔
感谢您的回答!这样,我只需向我的客户添加订单,但不会刷新我的ObservableCollection。 – Flea777 2011-01-08 12:18:45