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

asp.net-core-2.0 asp.net-mvc c# entity-framework-core

Question

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);
1
0
1/23/2018 7:41:21 PM

Accepted Answer

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);
1
8/24/2018 12:55:50 PM


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