Mapper.Map Addressmodel.Citysmodel Endereco.City

Asked

Viewed 24 times

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??

  • 1

    You mapped the City and Citadewmodel classes? CreateMap<Cidade, CidadeViewModel>().ReverseMap();

  • I just did the mapping var end = Mapper.Map<IEnumerable<Endereco>, IEnumerable<EnderecoViewModel>>(_EnderecoServico.BuscarPorIdDoCliente(id)); how do I do that CreateMap<Cidade, CidadeViewModel>().ReverseMap();, is in the msm controller??

  • 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.

  • 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, the ProfileName ta thus: get { return "ViewModelToDomainMappingProfile"; } and in the ViewModelToDomainMappingProfile all the same --- > get { return "ViewModelToDomainMappingToProfile"; }

  • Na, vdd, continue in the same way, the EnderecoViewModel.CidadeViewModel still coming null

  • 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.

  • I put the class in the question

Show 2 more comments

1 answer

1


Automapper is unable to convert to Cidade why in class EnderecoViewModel the property is as CidadeViewModel instead of Cidade.

  public class EnderecoViewModel
{
    public virtual CidadeViewModel Cidade { get; set; }
    public int CidadeId { get; set; }

}

 public class Endereco
{
    public int Id { get; set; }
    public virtual Cidade Cidade { get; set; }
    public int CidadeId { get; set; }
}
  • Perfect, that’s right... it’s the first time I’ve ever worked with Automapper.. Thanks for the strength!

  • 1

    For nothing @Rafaelpassos After I copied the classes here, I went to see that it was the name of the property. Thanks!

Browser other questions tagged

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