I am trying to remove the same row from the existing table PendingTestResult which is added in TestResult table. but it's not working kindly help me out to resolve this
this is controller
[HttpPost]
[Route("Reception/PatientTests/SaveTestResult")]
public async Task<IActionResult> SaveTestResult(List<TestResult> testResults)
{
if (ModelState.IsValid)
{
foreach (TestResult tr in testResults)
{
_db.Add(tr);
await _db.SaveChangesAsync();
var TestResultId = new PendingTestResult { Id = tr.Id };
_db.Remove<PendingTestResult>(TestResultId);
await _db.SaveChangesAsync();
}
// return new JsonResult("Index");
}
return new JsonResult(testResults); ;
}
here i want to remove same rows from PendingTestResult table which is newly added in TestResult Table.
please make sure the data you want to delete is available in the table.
instead of creating a new object, I would recommend using the Find method
assuming _db is the object of DbContext.
_db.Add(tr);
_db.PendingTestResults.Remove(_db.PendingTestResults.Find(tr.Id));
await _db.SaveChangesAsync();
please change the DbSet property name.
If you want to debug your code, use sql server profiler.