1
我有一個List<T>
綁定到一個數據網格,並試圖找出如何保存對數據庫的所有更改(使用實體框架),而不是一次一行;或者,至少有一種更好的方式可以將更改從數據網格提交到數據庫。我正在使用MVVM模式。 這裏是我儘可能節省一行:數據網格上的實體框架CRUD操作
private static PROJECT_EXPENSEEntities _service;
//The default constructor
public ProjectExpenseItemsRepository()
{
if (_service == null)
{
_service = new PROJECT_EXPENSEEntities();
}
}
public void AddProjectExpenseItems(int projectExpenseID)
{
project_expense_items p = new project_expense_items();
if (entityList != null)
{
p.project_expense_id = projectExpenseID;
p.item_number = entityList.ItemNumber;
p.item_description = entityList.ItemDescription;
p.item_unit_price = entityList.ItemUnitPrice;
p.item_qty = entityList.ItemQty;
p.supplier_name = entityList.SupplierName;
p.create_date = System.DateTime.Today;
_service.AddObject("project_expense_items", p);
_service.SaveChanges();
}
}
不過,我寧願在DataGrid更改發送到所有行一次:
public void AddProjectExpenseItems(List<ProjectExpenseItemsBO> entityList, int projectExpenseID)
{
project_expense_items p = new project_expense_items();
if (entityList != null)
{
// not sure what goes here, do I need a loop for each item in the list?
// I have a feeling I am going in the wrong direction with this...
_service.AddObject("project_expense_items", entityList);
_service.SaveChanges();
}
}
我還沒有發現網絡上有很多很好的例子。如果有人能指出我的例子,我會很感激。
如果你正在使用RIA有一個簡單的方法來做這件事。只需調用DomainContext類的SubmitChanges。 – 2011-11-09 18:27:15