0
I have the class Addressee
public class Endereco
{
public virtual Cidade Cidade { get; set; }
public int CidadeId { get; set; }
}
And I got the Viewmodel Of That Class:
public class EnderecoViewModel
{
public virtual CidadeViewModel CidadeViewModel { get; set; }
public int CidadeId { get; set; }
}
My Address class has a City. In my controller, I have a query like this:
var end = Mapper.Map<IEnumerable<Endereco>, IEnumerable<EnderecoViewModel>>(_EnderecoServico.BuscarPorIdDoCliente(id));
This query brings me the customer’s addresses.
The problem is in City Address, which is coming Null.
o Endereco.Cidadeid is coming correctly.how do I do this mapping using Mapper? if I staunch the entity, and assign its values: entidadeViewModel.CidadeViewModel.Nome = entidade.Cidade.Nome
, works, but how do I do it with Mapper?
This is the query in the Data Layer.Repository:
public class EnderecoRepositorio : RepositorioBase<Endereco>, IEnderecoRepositorio
{
public IEnumerable<Endereco> BuscarPorIdDoCliente(int? id)
{
return db.EnderecoDb.Where(e => e.ClienteId == id);
}
}
Class Configuration Mapper:
public class DomainToViewModelMappingProfile: Profile
{
public override string ProfileName
{
get { return "ViewModelToDomainMappingProfile"; }
}
protected override void Configure()
{
Mapper.CreateMap<ClienteViewModel, Cliente>();
Mapper.CreateMap<CidadeViewModel, Cidade>();
Mapper.CreateMap<ComplementoPessoaFisicaViewModel, ComplementoPessoaFisica>();
Mapper.CreateMap<ContadorViewModel, Contador>();
Mapper.CreateMap<ContatoViewModel, Contato>();
Mapper.CreateMap<ControleDeCobrancaViewModel, ControleDeCobranca>();
Mapper.CreateMap<ControleDeVencimentoViewModel, ControleDeVencimento>();
Mapper.CreateMap<ConveniadosViewModel, Conveniados>();
Mapper.CreateMap<EnderecoViewModel, Endereco>();
Mapper.CreateMap<ParceiroComercialViewModel, ParceiroComercial>();
Mapper.CreateMap<RamoDeAtividadeViewModel, RamoDeAtividade>();
Mapper.CreateMap<RegistroDeBloqueiosViewModel, RegistroDeBloqueios>();
Mapper.CreateMap<TipoDeSistemasViewModel, TipoDeSistemas>();
Mapper.CreateMap<UsuarioViewModel, Usuario>();
}
}
Proxima
public class ViewModelToDomainMappingProfile : Profile
{
public override string ProfileName
{
get { return "DomainToViewModelMappingProfile"; }
}
protected override void Configure()
{
Mapper.CreateMap<Cliente, ClienteViewModel>();
Mapper.CreateMap<Cidade, CidadeViewModel>();
Mapper.CreateMap<ComplementoPessoaFisica, ComplementoPessoaFisicaViewModel>();
Mapper.CreateMap<Contador, ContadorViewModel>();
Mapper.CreateMap<Contato, ContatoViewModel>();
Mapper.CreateMap<ControleDeCobranca, ControleDeCobrancaViewModel>();
Mapper.CreateMap<ControleDeVencimento, ControleDeVencimentoViewModel>();
Mapper.CreateMap<Conveniados, ConveniadosViewModel>();
Mapper.CreateMap<Endereco, EnderecoViewModel>();
Mapper.CreateMap<ParceiroComercial, ParceiroComercialViewModel>();
Mapper.CreateMap<RamoDeAtividade, RamoDeAtividadeViewModel>();
Mapper.CreateMap<RegistroDeBloqueios, RegistroDeBloqueiosViewModel>();
Mapper.CreateMap<TipoDeSistemas, TipoDeSistemasViewModel>();
Mapper.CreateMap<Usuario, UsuarioViewModel>();
}
}
I debugged Cód, and I realized, in the query, in the repository layer, it’s bringing the CITY, Just not transforming this Address.City in Addressremodel.CidadeViewModel... there is some mapping that should be done??
You mapped the City and Citadewmodel classes?
CreateMap<Cidade, CidadeViewModel>().ReverseMap();
– Raquel Pinheiro
I just did the mapping
var end = Mapper.Map<IEnumerable<Endereco>, IEnumerable<EnderecoViewModel>>(_EnderecoServico.BuscarPorIdDoCliente(id));
how do I do thatCreateMap<Cidade, CidadeViewModel>().ReverseMap();
, is in the msm controller??– Rafael Passos
You create a configuration class to register Automapper and register this global class. Is your project ASP.NET MVC Core? I will post both ways to register this Automapper configuration class.
– Raquel Pinheiro
I had already done it.. but thanks to you, I went to look again, and I think I found the mistake.... in my class
DomainToViewModelMappingProfile
, theProfileName
ta thus:get { return "ViewModelToDomainMappingProfile"; }
and in theViewModelToDomainMappingProfile
all the same --- >get { return "ViewModelToDomainMappingToProfile"; }
– Rafael Passos
Na, vdd, continue in the same way, the
EnderecoViewModel.CidadeViewModel
still coming null– Rafael Passos
Can you put the configuration class in the question? Something is missing... Does the City class have similar properties to viewModel? Send those classes too, please.
– Raquel Pinheiro
I put the class in the question
– Rafael Passos