I saw this error, "Error: read ECONNRESET", on Postman's Console after changing my controller to insert data into the database via _context.AddRange()
. Previously, I added data via _context.Entity.Add()
and it was fine.
I changed the insertion method to be able to add a Lista (list) entity to the database while also inserting a list of n Pessoa (person) entities and n relations (list x person) to the database.
The inserts were all successfully made, although Postman still "Could not get any response".
Any idea why this is happening?
public async Task<IActionResult> PostListas(ListaFileInput input)
{
...
Listas lista = new Listas
{
OwnerId = userId,
Lista = input.Lista
};
...
List<Pessoas> pessoas = csv.GetRecords<Pessoas>().ToList();
foreach (Pessoas pessoa in pessoas)
{
pessoa.OwnerId = userId;
_context.AddRange(new ListasXPessoas() { Lista = lista, Pessoa = pessoa });
}
await _context.SaveChangesAsync();
return CreatedAtAction("GetListas", new { id = lista.ListaId }, lista);
答案 0 :(得分:1)
ECONNRESET是一个很好的指标,在执行离开控制器后抛出了一些异常。检查输出窗口,我看到异常是:
检测到自我参考循环
然后我发现它是由返回对象的JSON序列化引起的。
最终,它通过添加以下代码来解决,以忽略循环验证:
services.AddMvc().AddJsonOptions(opt =>
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);