1
votes

I'm using automapper in my project and until today was with the very old version of it and I decided to update it to the latest version.

When executing my project and testing some apis, some worked normally (without relationships), but others appeared the following error:

Error mapping types.

Mapping types: PaginaModelCadastro -> Pagina Identidade.App.Models.PaginaModelCadastro -> Identidade.Domain.Entities.Pagina

Type Map configuration: PaginaModelCadastro -> Pagina Identidade.App.Models.PaginaModelCadastro -> Identidade.Domain.Entities.Pagina Property: IdDominio

Following is all classes and mappings that refer to the error:

** Automapper configuration class **

    public static void Configure()
    {
        Mapper.Initialize(map =>
        {
            map.AddProfile<EntityToModelMapping>();
            map.AddProfile<ModelToEntityMapping>();
        });
    }

Mapping from model to entity

        CreateMap<PaginaModelCadastro, Pagina>()
        .ForMember(dest => dest.IdDominio, src => src.MapFrom(m => new Dominio() { IdDominio = m.IdDominio }));

Class: Dominio

public class Dominio
{
    public virtual int IdDominio { get; set; }
    public virtual string Descricao { get; set; }

    public virtual ICollection<Pagina> Paginas { get; set; }
}

Class: Pagina

public class Pagina
{
    public virtual int IdPagina { get; set; }
    public virtual string Descricao { get; set; }
    public virtual int IdDominio { get; set; }

    public virtual Dominio Dominio { get; set; }
    public virtual ICollection<Permissao> Permissoes { get; set; }
}
1

1 Answers

0
votes

There is no way to map between IdDominio and Dominio. You have to remove that ForMember you have. Maybe you meant

    CreateMap<PaginaModelCadastro, Pagina>()
    .ForMember(dest => dest.Dominio, src => src.MapFrom(m => new Dominio() { IdDominio = m.IdDominio }));