The AutoMapper
works to map a single class into another single class. So, if in your Viewmodel you will have information from a group and a user, I suggest you create a class that has a reference for each of them. Something that will be instantiated like this:
var usuarioGrupo = new UsuarioGrupo
{
Usuario = usuario,
Grupo = grupo
}
From there, I see two possibilities:
1) You will get your class UsuarioGrupoViewModel
with references to two other Viewmodel classes: UsuarioViewModel
and GrupoViewModel
. That is, to access the name of the group, you will search in usuarioGrupoViewModel.Grupo.Nome
.
2) You can also create the Group Name and User fields and configure a Profile
of AutoMapper
to fill these fields, with something like this:
Mapper.CreateMap<UsuarioGrupo, UsuarioGrupoViewModel>()
.ForMember(d => d.NomeGrupo, o => o.MapFrom(s => s.Grupo.Nome))
.ForMember(d => d.NomeUsuario, o => o.MapFrom(s => s.Usuario.Nome))
.ReverseMap();
I usually use both approaches.
Can post here the view model and entity code you are trying to map to?
– Felipe Assunção
Hello Felipe, I am responding from a machine that does not have the sources, but I need to show table data users and groups, the two tables have the NAME field, as I could create a viewmodel with these two equal fields?
– Alexander Leão dos Reis