标签:mod nal org include wait row nis start contex
一、首先在需要进行并发处理的字段上加上[Timestamp]标记:
public class Department { public int Id { get; set;} public int? InstructorId{ get; set; } public ICollection<Course> Courses {get; set; } [Timestamp] public byte[] RowVersion{get; set; } }
二、然后更新数据库
add-migration updateTimestampForDeparment
update-database
三、重新建基架项目
删除Create和Edit页面内关于RowVersion项目的输入项
四、打开编辑Edit的控制器,修改如下:
[HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int? id, byte[]rowVersion) { if(id==null) { return NoFound(); } //先查一下数据是否存在 var department= await _context.Departments.Include(a=>a.Administrator) .SingleOrDefaultAsync(a=>a.Id==id); if(department==null) { var deletedDepartment=new Department(); await TryUpdateModeAsync(deletedDepartment); ModelState.AddModelError(string.Empty,"无法进行数据的修改,该部门信息已经被其他人删除!); ViewData["InstructorId"]=new SelectList(_context.Instructors, "Id","RealName",deletedDepartment); return View(deletedDepartment); } //将RowVersion标志原来的值变更为新的值 _context.Entry(department).Property("RowVersion").OriginalValue = rowVersion; if(await TryUpdateModelAsync<Department>(department, a=>a.Name, a=>a.StartDate, a=>a.Budge(a=>a.InstructorId)) { try { } catch() { } } }
标签:mod nal org include wait row nis start contex
原文地址:https://www.cnblogs.com/sky-net/p/9242775.html