要更新的SaveChanges()不起作用

时间:2010-09-22 08:15:54

标签: asp.net-mvc-2 entity-framework-4

我正在使用MVC 2和EF 4。

我正在尝试使用自己的存储过程更新我的Application实体,但它没有更新。我检查了SQL事件探查器,它甚至没有到达数据库。我也使用插入存储过程,它工作正常。两个存储过程(插入和更新)都已正确映射。有谁知道我做错了什么?

以下是我的EditApplication操作事件:

[HttpPost]
public ActionResult EditApplication(int id, ApplicationViewModel applicationViewModel)
{
   if (ModelState.IsValid)
   {
      try
      {
         Mapper.CreateMap<ApplicationViewModel, Application>();
         var application = (Application)Mapper.Map(applicationViewModel, typeof(ApplicationViewModel), typeof(Application));
         application.ApplicationID = id;

         UpdateModel(application);

         // Edit application
         applicationRepository.UpdateApplication(application);

         // If success, redirect to confirmation page
         return RedirectToAction("Confirmation", new { id = application.ApplicationID });
      }
      catch (Exception ex)
      {
         // TODO
      }
   }

   applicationViewModel.AccountTypes = accountTypeRepository.GetAccountTypes();
   return View("CreateApplication", applicationViewModel);
}

这是我的存储库更新方法:

private MyEntities db = new MyEntities();

public void UpdateApplication(Application application)
{
   db.SaveChanges();
}

由于 布伦丹

1 个答案:

答案 0 :(得分:0)

我的更改,不确定它是否正确,但它正在运行。

动作事件:

[HttpPost]
public ActionResult EditApplication(int id, ApplicationViewModel applicationViewModel)
{
   var application = applicationRepository.GetApplicationByID(id);

   if (ModelState.IsValid)
   {
      try
      {
         Mapper.CreateMap<ApplicationViewModel, Application>();
         application = (Application)Mapper.Map(applicationViewModel, typeof(ApplicationViewModel), typeof(Application));

         application.ApplicationStateID = 1;
         application.ApplicationID = id;

         // Edit application
         applicationRepository.UpdateApplication(application);

         // If success, redirect to confirmation page
         return RedirectToAction("Confirmation", new { id = application.ApplicationID });
      }
      catch (Exception ex)
      {
         // TODO
      }
   }

   applicationViewModel.AccountTypes = accountTypeRepository.GetAccountTypes();
   return View("CreateApplication", applicationViewModel);
}

我的存储库中的更新方法:

private MyEntities context = new MyEntities();

public void UpdateApplication(Application application)
{
   context.Applications.ApplyCurrentValues(application);
   context.SaveChanges();
}