2011-08-24 76 views
0
public ActionResult Edit(int id) 
{ 
    var productBrand = brandRepo.FindProductBrand(id); 
    ProductBrandModel model = Mapper.Map<ProductBrand, ProductBrandModel>(productBrand); 
    return View(model); 
} 

[HttpPost] 
public ActionResult Edit(ProductBrandModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     var productBrand = brandRepo.FindProductBrand(model.BrandId); 
     productBrand.Name = model.Name;    
     //How to persist that information? 
    } 
} 

我有一個EF產生類ProductBrand並呼籲ProductBrandModel的視圖的模型。如何在MVC3中保存編輯?

我將如何使用實體框架持久化編輯信息?如果我brandRepo有一個名爲SaveChanges空洞的方法時,在那裏我會去:

public void SaveChanges() 
{ 
    dbEntities.SaveChanges(); 
} 

回答

1

正如你正確地假設,你必須提交更改使用.SaveChanges()方法的數據庫。在你的情況下,brandRepo.SaveChanges()將委託給dbEntities.SaveChanges()

補充說明:在簡單的情況下,獨立的存儲設備類只增加了複雜性並沒有真正帶來任何益處。實體框架的DbContext與簡單的存儲庫本身非常相似,因此您不需要其中一個。

當然,爲了可測試性,間接層可能是有意義的。

沒有倉庫的代碼可能看起來有點像這樣:

public ActionResult Edit(int id) 
{ 
    var productBrand = dbEntities.ProductBrands.Find(x => x.BrandId = id); 
    ProductBrandModel model = Mapper.Map<ProductBrand, ProductBrandModel>(productBrand); 
    return View(model); 
} 

[HttpPost] 
public ActionResult Edit(ProductBrandModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     var productBrand = dbEntities.ProductBrands.Find(x => x.BrandId = id); 
     // or something similar, I don't know the inner workings of your 
     // brandRepo.FindProductBrand(id) 

     productBrand.Name = model.Name;    
     dbEntities.SaveChanges(); 
    } 
} 
0

我喜歡在我的倉庫保存方法與實體框架的輔助方法我從網上得到了一起。 SaveCustomer是我的存儲庫類方法,它下面是輔助類。在你的情況,你會通過你的模型到

brandRepository.SaveProdctBrand(productBrand) 

(有助於闡明良好的命名規則和的FxCop規則的名稱)

public void SaveCustomer(Customer customer) 
{ 
    using (var ctx = new WebStoreEntities()) 
    { 
    if (customer.CustomerId > 0) 
    { 
     //It's an existing record, update it. 
     ctx.Customers.AttachAsModified(customer); 
     ctx.SaveChanges(); 
    } 
    else 
    { 
     //its a new record. 
     ctx.Customers.AddObject(customer); 
     ctx.SaveChanges(); 
    } 
    } 
} 

的輔助類如下

public static class EntityFrameworkExtensions 
{ 
/// <summary> 
/// This class allows you to attach an entity. 
/// For instance, a controller method Edit(Customer customer) 
/// using ctx.AttachAsModified(customer); 
/// ctx.SaveChanges(); 
/// allows you to easily reattach this item for udpating. 
/// Credit goes to: http://geekswithblogs.net/michelotti/archive/2009/11/27/attaching-modified-entities-in-ef-4.aspx 
/// </summary> 
public static void AttachAsModified<T>(this ObjectSet<T> objectSet, T entity) where T : class 
{ 
    objectSet.Attach(entity); 
    objectSet.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); 
} 

/// <summary> 
/// This marks an item for deletion, but does not currently mark child objects (relationships). 
/// For those cases you must query the object, include the relationships, and then delete. 
/// </summary> 
/// <typeparam name="T"></typeparam> 
/// <param name="objectSet"></param> 
/// <param name="entity"></param> 
public static void AttachAsDeleted<T>(this ObjectSet<T> objectSet, T entity) where T : class 
{ 
    objectSet.Attach(entity); 
    objectSet.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted); 
} 

public static void AttachAllAsModified<T>(this ObjectSet<T> objectSet, IEnumerable<T> entities) where T : class 
{ 
    foreach (var item in entities) 
    { 
     objectSet.Attach(item); 
     objectSet.Context.ObjectStateManager.ChangeObjectState(item, EntityState.Modified); 
    } 
} 
}