How to solve Stackoverflow exception that occurs in Entity Mapping for Model?

1

In my application a Team has several users and a User belongs only to a Team (1: N), so I have the following entities:

public class Equipe{

     public Guid EquipeId {get; set;}
     public string Nome {get; set;}
     public virtual ICollection<Usuario> Usuarios {get; set;}

     public Equipe(){
          EquipeId = Guid.NewGuid();
          Usuarios = new List<Usuarios>();
      }
}

public class Usuario{

     public Guid UsuarioId {get; set;}
     public string Nome {get; set;}

     public Guid EquipeId {get; set;}
     public virtual Equipe {get; set;}

     public Usuario(){
          UsuariosId = Guid.NewGuid();
          Equipe = new Equipe();
     }
}

In the Team entity settings I'm using the following code:

public class EquipeConfig : EntityTypeConfiguration<Equipe>{

   public EquipeConfig(){

          ToTables("Equipes");

          HasKey(x => x.EquipeId);

          Property(x => x.Nome)
               .IsRequired().
               .HasMaxLenght(250);

         HasMany(x => x.Usuarios)
               .WithRequired(c => c.Equipe)
               .HasForeingKey(x => x.EquipeId);
   }
}
But when I query the repository to get all the users and their respective teams, I have this exception when Mapper tries to map to Model :

  

Stackoverflow Exception

//método do repositório
public ICollection<Usuarios> ObtenhaTodosUsuarios()
      => Context.Usuarios.Include(c => c.Equipe).ToList();

//método da Application      
public ICollection<UsuarioModel> ObtenhaTodosUsuarios(){

      var result = _UsuarioRepositorio.ObtenhaTodosUsuarios();

      return Mapper.Map<ICollection<UsuarioModel>>(result); //exceção

}

In context the following settings in the constructor:

Configuration.LazyLoadingEnabled = false;
Configuration.AutoDetectChangesEnabled = false;
Configuration.ProxyCreationEnabled = false;

And no override OnModelCreating

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();                
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();                
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
    
asked by anonymous 18.05.2018 / 00:51

0 answers