我在ASP.Net核心MVC中有一個Web API控制器。爲什麼修改從實體框架核心返回的本地實體後,我的執行消失?
它看起來像這樣:
namespace Hrsa.Core.Web.App.Controllers.Api
{
[Route("api/[controller]")]
public class CasesController : Controller
{
private readonly IGenericService<Case> _caseGenericService;
public CasesController(IGenericService<Case> caseGenericService)
{
_caseGenericService = caseGenericService;
}
[HttpPost]
public async Task<Case> AddCase([FromBody] EmployeeCaseFromBodyModel model)
{
Case employeeCase = new Case()
{
InitalConsultDate = model.InitialConsultDate,
};
Case newCase = await _caseGenericService.InsertAsync(employeeCase);
EmployeeCase empToCase = new EmployeeCase
{
CaseId = newCase.Id,
EmployeeId = model.EmployeeId
};
newCase.EmployeeCases.Add(empToCase);
await _caseGenericService.UpdateAsync(newCase);
return newCase;
}
}
}
我得到我的新情況下,從通用服務了。 而我的新案件有一個新生成的ID。大。
現在我創建一個新的EmployeeCase記錄,其中包含我新生成的案例的id以及我想要添加案例的員工ID。
現在我試着簡單地將Employee案例記錄添加到EmployeeCases集合中。
但執行只是被吞併消失。 我在輸出窗口中得到一堆線程退出的行。
The thread 0x2128 has exited with code 0 (0x0).
The thread 0x2e38 has exited with code 0 (0x0).
The thread 0x2d5c has exited with code 0 (0x0).
The thread 0x2d44 has exited with code 0 (0x0).
The thread 0x2658 has exited with code 0 (0x0).
一旦新案例被插入並由Entity Framework返回,是不是newCase變量的局部變量? 我不應該只是能夠像這樣修改它嗎?
執行過程在哪裏? 在調試的時候,當我走到下一行時,它只是一直給我線程退出行,永遠不會去下一行。
實體仍然可以以某種方式附加到dbContext並且不被放棄? 這就是爲什麼它不會讓我在當地工作?
我想我可以像這樣在本地修改它,然後在將EmployeeCase記錄添加到它之後發送整個實體進行更新。 但在進入更新行之前,它會被吞噬掉。
更新1:
現在我在所有之前的輸出窗口中看到「線程NNN已退出,代碼爲0」線:
Exception thrown: 'System.NullReferenceException' in Hrsa.Core.Web.App.dll
因此,一些是空的這條線:
newCase.EmployeeCases.Add(empToCase);
但我不知道是什麼。