ASP.NET MVC - DbUpdateConcurrencyException

时间:2017-11-20 12:55:03

标签: asp.net-mvc

我在我的应用程序中有这个流程

ViewModel

   public class CountriesViewModel
{
        public CountriesViewModel()
        {
            // TODO: Complete member initialization
            CREATED_DATE = DateTime.Now;
            CREATED_BY = int.Parse(System.Web.HttpContext.Current.User.Identity.GetUserId().ToString());
            LAST_UPDATE_DATE = DateTime.Now;
            LAST_UPDATE_BY = int.Parse(System.Web.HttpContext.Current.User.Identity.GetUserId().ToString());
            DELETED_DATE = DateTime.Now;
            DELETED_BY = int.Parse(System.Web.HttpContext.Current.User.Identity.GetUserId().ToString());
        }

        [HiddenInput]
        public int COUNTRY_ID { get; set; }

        [Required(ErrorMessage = "Country Code is required")]
        [Display(Name = "Country Code")]
        [StringLength(2, ErrorMessage = "The {0} must be at least {1} characters long. Plese check again!", MinimumLength = 2)]
        public string COUNTRY_CODE { get; set; }

        [Required(ErrorMessage = "Country Name is required")]
        [Display(Name = "Country Name")]
        //[Index(IsUnique = true)]
        [StringLength(150, ErrorMessage = "The {0} must be at least {1} characters long. Plese check again!", MinimumLength = 2)]
        public string COUNTRY_NAME { get; set; }


        [Display(Name = "Action Status")]
        public int ACTION_STATUS { get; set; }

        [Display(Name = "Date Created")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]  //[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
        public Nullable<System.DateTime> CREATED_DATE { get; set; }


        [Display(Name = "Created By")]
        public Nullable<int> CREATED_BY { get; set; }


        [Display(Name = "Last Update Date")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]  //[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
        public Nullable<System.DateTime> LAST_UPDATE_DATE { get; set; }


        [Display(Name = "Last Update By")]
        public Nullable<int> LAST_UPDATE_BY { get; set; }


        [Display(Name = "Date Deleted")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]  //[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
        public Nullable<System.DateTime> DELETED_DATE { get; set; }


        [Display(Name = "Deleted By")]
        public Nullable<int> DELETED_BY { get; set; }

}

存储库调用ViewModel。

  

我认为错误来自存储库。

存储库

public void EditCountry(CountriesViewModel countryModel)
    {

        COUNTRIES2 newCountry = new COUNTRIES2()
        {
            COUNTRY_ID = countryModel.COUNTRY_ID,
            COUNTRY_CODE = countryModel.COUNTRY_CODE,
            COUNTRY_NAME = countryModel.COUNTRY_NAME,
            ACTION_STATUS = countryModel.ACTION_STATUS,
            LAST_UPDATE_BY = countryModel.LAST_UPDATE_BY,
            LAST_UPDATE_DATE = countryModel.LAST_UPDATE_DATE
        };
        entity.COUNTRIES.Add(newCountry);
        entity.Entry(newCountry).State = EntityState.Modified;
        entity.SaveChanges();
    }

    public CountriesViewModel ViewCountryDetails(int countryId)
    {
        var singleCountryDetail = entity.COUNTRIES.Where(x => x.COUNTRY_ID == countryId).FirstOrDefault();
        CountriesViewModel modelObj = new CountriesViewModel()
        {
            COUNTRY_ID = singleCountryDetail.COUNTRY_ID,
            COUNTRY_CODE = singleCountryDetail.COUNTRY_CODE,
            COUNTRY_NAME = singleCountryDetail.COUNTRY_NAME,
            ACTION_STATUS = (int)singleCountryDetail.ACTION_STATUS

        };
        return modelObj;
    }

这些是下面显示的控制器。

控制器

        public ActionResult Edit(int id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var details = countryRepo.ViewCountryDetails(id);
        return View(details);
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(int id, CountriesViewModel countries)
    {
        if (ModelState.IsValid)
        {
                // TODO: Add update logic here
                countryRepo.EditCountry(countries);
                var notif = new UINotificationViewModel()
                {
                    notif_message = "Record updated successfully",
                    notif_type = NotificationType.SUCCESS,
                };
                TempData["notif"] = notif;
                return RedirectToAction("Index");

        }
        return View(countries);
    }

当我点击进行编辑时,我收到了以下错误

UpdateConcurrency

请问如何解决此问题。 Create Controller正在运行,但它只有编辑控制器不能正常工作

1 个答案:

答案 0 :(得分:0)

这可能是因为同一个记录由多个用户同时编辑。最好把你的代码放在事务中并提交它。

using (var contextTransaction = _someDbContext.Database.BeginTransaction())
            {
//code
_someDbContext.SaveChanges();
contextTransaction.Commit();
}