How to add data and remove same data from other table in asp.net core?

asp.net-core asp.net-core-2.0 entity-framework-core

Question

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.

1
0
4/14/2020 7:08:09 AM

Accepted Answer

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.

0
4/14/2020 7:21:58 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow