Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ECONNRESET after CreatedAtAction, using _context.AddRange() - EF 2.0

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);
like image 211
Matheus Lacerda Avatar asked Sep 15 '25 02:09

Matheus Lacerda


1 Answers

ECONNRESET is a good indicator that some exception was thrown after execution left the controller. Examining the output window, I saw that the exception was:

Self referencing loop detected

I then found out that it was caused by the serialization to JSON of the returning object.

Ultimately, it was solved by adding the following code to ignore the loop verification:

services.AddMvc().AddJsonOptions(opt => 
    opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
like image 163
Matheus Lacerda Avatar answered Sep 17 '25 16:09

Matheus Lacerda