Asp.net mvc 3 如何在MVC3中保持编辑?

Asp.net mvc 3 如何在MVC3中保持编辑?,asp.net-mvc-3,entity-framework-4,edit,Asp.net Mvc 3,Entity Framework 4,Edit,正如您正确假设的那样,您必须使用.SaveChanges()方法将更改提交到数据库。在您的情况下,brandRepo.SaveChanges()将委托给dbEntities.SaveChanges() 附带说明:在简单的情况下,单独的存储库类只会带来复杂性,而不会带来任何好处。实体框架的DbContext非常类似于一个简单的存储库本身,所以您不需要在上面放一个 当然,为了可测试性,一个间接层可能是有意义的 如果没有存储库,您的代码可能看起来像这样: public void SaveChanges

正如您正确假设的那样,您必须使用
.SaveChanges()
方法将更改提交到数据库。在您的情况下,
brandRepo.SaveChanges()
将委托给
dbEntities.SaveChanges()

附带说明:在简单的情况下,单独的存储库类只会带来复杂性,而不会带来任何好处。实体框架的
DbContext
非常类似于一个简单的存储库本身,所以您不需要在上面放一个

当然,为了可测试性,一个间接层可能是有意义的

如果没有存储库,您的代码可能看起来像这样:

public void SaveChanges()
{
    dbEntities.SaveChanges();
}
public ActionResult编辑(int-id)
{
var productBrand=dbEntities.ProductBrands.Find(x=>x.BrandId=id);
ProductBrandModel=Mapper.Map(productBrand);
返回视图(模型);
}
[HttpPost]
公共行动结果编辑(ProductBrandModel)
{
if(ModelState.IsValid)
{
var productBrand=dbEntities.ProductBrands.Find(x=>x.BrandId=id);
//或者类似的,我不知道你的大脑的内部运作
//brandRepo.FindProductBrand(id)
productBrand.Name=model.Name;
dbEntities.SaveChanges();
}
}

我希望在我的存储库中有一个save方法和一个从网上获得的entity framework助手方法。SaveCustomer是我的存储库类方法,下面是helper类。在您的情况下,您可以将您的模型传递到

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();
    }
}
(有助于拼写良好命名约定和fxcop规则的名称)

helper类如下所示

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();
    }
  }
}
公共静态类EntityFrameworkExtensions
{
/// 
///此类允许您附加实体。
///例如,控制器方法Edit(Customer)
///使用ctx.AttachAsModified(客户);
///ctx.SaveChanges();
///允许您轻松地重新连接此项目以进行udpating。
///归功于:http://geekswithblogs.net/michelotti/archive/2009/11/27/attaching-modified-entities-in-ef-4.aspx
/// 
public static void AttachAsModified(此ObjectSet ObjectSet,T entity),其中T:class
{
objectSet.Attach(实体);
objectSet.Context.ObjectStateManager.ChangeObjectState(实体,EntityState.Modified);
}
/// 
///这会标记要删除的项,但当前不会标记子对象(关系)。
///对于这些情况,必须查询对象,包括关系,然后删除。
/// 
/// 
/// 
/// 
public static void attachesdeleted(此ObjectSet ObjectSet,T entity),其中T:class
{
objectSet.Attach(实体);
objectSet.Context.ObjectStateManager.ChangeObjectState(实体,EntityState.Deleted);
}
公共静态void attachalasmodified(此ObjectSet ObjectSet,IEnumerable entities),其中T:class
{
foreach(实体中的var项)
{
objectSet.Attach(项目);
objectSet.Context.ObjectStateManager.ChangeObjectState(项,EntityState.Modified);
}
}
}
brandRepository.SaveProdctBrand(productBrand)
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);
    }
}
}