1
I have the following classes:
public class ListaGrupo : EntidadeBase
{
public ListaGrupo()
{
Items = new List<ListaGrupoItem>();
}
public int Id { get; set; }
public string Nome { get; set; }
public virtual ICollection<ListaGrupoItem> Items { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("Id = " + Id + ";");
sb.Append("Nome = " + Nome + ";");
return sb.ToString();
}
}
public class ListaGrupoItem : EntidadeBase
{
public int Id { get; set; }
public int ListaGrupoId { get; set; }
public string Nome { get; set; }
public virtual ListaGrupo Grupo { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("Id = " + Id + ";");
sb.Append("Nome = " + Nome + ";");
sb.Append("GrupoId = " + ListaGrupoId + ";");
sb.Append("Grupo: {" + Grupo.ToString() + "};");
return sb.ToString();
}
}
In the EF configuration they are like this:
public class ListaGrupoConfiguracao : EntityTypeConfiguration<ListaGrupo>
{
public ListaGrupoConfiguracao()
{
// Chave Primária
HasKey(t => t.Id);
// Propriedades
Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.Nome).IsRequired().HasMaxLength(100);
// Mapeamento para as tabelas do banco
ToTable("lista_grupo");
Property(t => t.Id).HasColumnName("id");
Property(t => t.Nome).HasColumnName("nome");
// Relacionamentos
}
}
public class ListaGrupoItemConfiguracao : EntityTypeConfiguration<ListaGrupoItem>
{
public ListaGrupoItemConfiguracao()
{
// Chave Primária
HasKey(t => t.Id);
// Propriedades
Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.Nome).IsRequired().HasMaxLength(100);
// Mapeamento para as tabelas do banco
ToTable("lista_grupo_item");
Property(t => t.Id).HasColumnName("id");
Property(t => t.ListaGrupoId).HasColumnName("id_grupo");
Property(t => t.Nome).HasColumnName("nome");
// Relacionamentos
HasRequired(i => i.Grupo).WithMany(g => g.Items).HasForeignKey(i => i.ListaGrupoId);
// 1 para N
//HasMany<Estabelecimento>(i => i.EstabelecimentoTipo).WithRequired(e => e.Tipo).HasForeignKey(e => e.TipoId);
}
}
Finally in Controller I’m doing the cast:
public ActionResult Index()
{
try
{
var x = _listaGrupoItemAppServico.BuscarTodos();
var listaGrupoItemViewModel = Mapper.Map<IEnumerable<ListaGrupoItem>, IEnumerable<ListaGrupoItemViewModel>>(x);
return View(listaGrupoItemViewModel);
}
catch (Exception ex)
{
log.Error("Erro no Index do controller Estabelecimento", ex);
return View();
}
}
When running Mapper.Map an error and the application stops running, nor does the catch fall. If in my mapping I leave so:
HasRequired(i => i.Grupo).WithMany().HasForeignKey(i => i.ListaGrupoId);
It does not error but creates in the base another Listgroup_id with Foreign key. I’m already days with this problem. Has anyone been through this? I already downgrade the Autommaper and did not solve.
Error message:
An unhandled exception of type 'System.StackOverflowException' occurred in AutoMapper.dll
looks an example that causes problems to method rewriting
ToString()
.– novic
So like I said when I removed Withmany’s parameter, he’s cool with it
– Carlos Elias S. do Carmo
When you take out the setting
WithMany
you end up losing what?– novic
He creates a new column at my base and does not obey what I have determined. For example I have the columns Id, Name, Id_group that are mapped to my objects as mentioned above. When deleting the parameters it creates a fourth column.
– Carlos Elias S. do Carmo
That’s right, because, you rewrite the
ToString()
Where did you see this for Entityframework? (particularly I think it’s a mistake and I’ve never seen this kind of implementation)– novic
The superscripted Tostring is my business rule, not Entity. And as I said when I removed Withmany’s parameter with an overwritten toString, he’d be fine.
– Carlos Elias S. do Carmo
Good Carlos I will give a survey if I know the answer put to you, I have never seen rewrite the method to ORM because it is the class used, but all right I will not affirm that it is wrong and not right without testing. and maybe it’s not even that.
– novic
Okay Virgilio thank you.
– Carlos Elias S. do Carmo
Good afternoon, var x = _listaGrupoItemAppServico.Buscartodos(); This variable comes from what type? My fear is the fact that he is still with the entityframework session, and at the time of conversion, because it is of the type
iqueryable
, is conducting circular consultations.– Joao Araújo
I change with the passing of the layers the call but the basis is this:
– Carlos Elias S. do Carmo
public Ienumerable<Tentity> Getall() { Try { var return = Db.Set<Tentity>(). Tolist(); Return return; } catch (Exception ex) { log.Error("Error Getall method in " + typeof(Tentity).Name, ex); Return null; } }
– Carlos Elias S. do Carmo