我尝试了许多不同的方法来删除IncidentDescriptions
DBContext的某些行,但是我真的无法弄清楚这一点。在调试窗口中,从该行显示的是一个ID和一个打印出来的描述,所以我知道有一个行被选中并且不会成为问题:
foreach (var desc in DB.IncidentDescriptions.Where(d => d.AccidentHeaderId == supervisor.AccidentHeaderId)) {
System.Diagnostics.Debug.WriteLine(desc.Id + " " + desc.Description);
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = @"Id,AccidentHeaderId,DateLastModified,LastModifiedUsername,DateAccidentReported,EmployeeReported,ReportedInSameShift
,DateReportedOutsideShift,AccidentLocation,IncidentDate,IncidentTime,MedicalAttention,Clinic,ReturnToWork,DrugScreenPapers,DamageDesc,MissDollarEst,ActsContributed
,CorrectiveActions,DiscusedWithEmployee,WorkOrderComplete,Comments,AccidentNarrative,FirstName,LastName,DrugTested,FirstNameReportedTo,LastNameReportedTo
,DateInvestigationStarted,TimeInvestigationStarted,DeptManager, HospitalName,
BodyPartXREF, EmployeesInvolved, IncidentDescriptions, InjuryDescriptionXREF, UnsafeActXREF, WitnessesInvolved
")] AccidentSupervisorViewModel supervisor, List<string> SelectedUnsafeActs, int? SelectedLocation, List<string>DescriptionUser, List<string>DescriptionPosition)
{
if (ModelState.IsValid)
{
using (DB)
{
using (var contextTransaction = DB.Database.BeginTransaction())
{
try
{
foreach (var desc in DB.IncidentDescriptions.Where(d => d.AccidentHeaderId == supervisor.AccidentHeaderId)) {
System.Diagnostics.Debug.WriteLine(desc.Id + " " + desc.Description);
}
DB.IncidentDescriptions.Remove(DB.IncidentDescriptions.FirstOrDefault(d => d.AccidentHeaderId == supervisor.AccidentHeaderId));
DB.SaveChanges();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
contextTransaction.Rollback();
}
}
}
return RedirectToAction("Index", "Incident");
}
return View(supervisor);
}
答案 0 :(得分:1)
您不是committing your transaction。顺便说一句,EF已经实施了工作单元,因此可能会过大。另外,我会用Single()代替FirstOrDefault()-如果什么也没找到,您想要一个异常。另外,不确定是否需要绑定。由于您具有视图模型,因此只会传递这些值。
我将正文重写为:
typeof AuthenticationService