简体   繁体   中英

How to correctly map Model to Entity in Automapper 6.1.1?

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; }
}

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 }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM