如何在 asp.net 核心中添加数据和从其他 table 删除相同数据?

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

我正在尝试从添加到 TestResult table 的现有 table PendingTestResult 中删除同一行。但它不起作用请帮我解决这个问题

这是控制器

 [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); ;
    }

这里我想从 PendingTestResult table 中删除相同的行,这是新添加到 TestResult Table.

请确保您要删除的数据在 table 中可用。
与其创建新对象,不如使用 Find 方法
假设_db是DbContext的对象。

 _db.Add(tr);
 _db.PendingTestResults.Remove(_db.PendingTestResults.Find(tr.Id));
  await _db.SaveChangesAsync();

请更改 DbSet 属性 名称。

如果要调试代码,请使用 sql 服务器分析器。