有时我们需要在事务中保存一些数据临时值。如果是的话 没有竞争,它将被删除。例如,上传图片。
最佳策略是什么?将数据保存在实际数据库中 执行任务计划,将删除不正常的数据(行)或保存它 在临时空间还是什么?
非常感谢。
答案 0 :(得分:1)
示例Temp数据,我希望我做得对。以busroute为例:-
if (id != busRoute.BusRouteCode)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(busRoute);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!BusRouteExists(busRoute.BusRouteCode))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(busRoute);
}
public async Task<IActionResult> Create([Bind("BusRouteCode,RouteName")] BusRoute busRoute)
{
if (ModelState.IsValid)
{
_context.Add(busRoute);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(busRoute);
}
对于索引:- TempData [“ message”] =“欢迎加入会员列表...”;
if (MemberId == null)
{
TempData["message"] = "Please select a Member...";
RedirectToAction("SHMember", "Index");
}
string memberId = MemberId.ToString();
var FullName = _context.Membership
.Include("FullName")
.Where(m => m.MemberId == MemberId)
.Select(m => m.Member.FullName)
.FirstOrDefault();
if (memberId != null)
{
HttpContext.Session.SetString("MemberId", memberId);
HttpContext.Session.SetString("FullName", FullName);
}
else
{
memberId = HttpContext.Session.GetString("MemberId");
FullName = HttpContext.Session.GetString("FullName");
if (memberId == null)
{
//return to View and Display Message
TempData["message"] = "Please select a Member...";
return RedirectToAction("Index", "Member");
}
}
ViewBag.FullName = HttpContext.Session.GetString("FullName");
var MembershipContext = _context.Membership
.Where(m => m.MemberId == MemberId)
.OrderByDescending(m=>m.Year);
return View(await MembershipContext.ToListAsync());