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

Asked

Viewed 28 times

1

In my application a Team has several users and a User belongs to only one 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 settings of the Team entity I am 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);
   }
}

However when I do the following query in the repository to get all users and their respective teams, I have this exception at the time when the Mapper tries to perform the mapping to the 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 constructor settings:

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

And in the override OnModelCreating

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();                
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();                
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
  • Where did you create the mapping rule? From what I’ve seen you’re using Automapper, correct?

  • My opinion: If you add the Entity Usuarios and UsuarioModel and how the mapping was done in the question is simpler to help you. I think the 3 would be enough to answer your question.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.