简体   繁体   English

如何在Automapper 6.1.1中将模型正确映射到实体?

[英]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. 我在项目中使用的是automapper,直到今天仍使用旧版本,因此我决定将其更新为最新版本。

When executing my project and testing some apis, some worked normally (without relationships), but others appeared the following error: 在执行我的项目并测试某​​些api时,某些api正常工作(没有关系),但是其他api出现以下错误:

Error mapping types. 错误映射类型。

Mapping types: PaginaModelCadastro -> Pagina Identidade.App.Models.PaginaModelCadastro -> Identidade.Domain.Entities.Pagina 映射类型: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 类型映射配置:PaginaModelCadastro-> Pagina Identidade.App.Models.PaginaModelCadastro-> Identidade.Domain.Entities.Pagina属性: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 类: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. 无法在IdDominio和Dominio之间进行映射。 You have to remove that ForMember you have. 您必须删除该ForMember。 Maybe you meant 也许你是说

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用AutoMapper创建模型到实体的映射以及实体到模型的映射? - How to create a map from model to Entity, and Entity to model, with AutoMapper? 使用AutoMapper 6.1.1创建地图 - Create Map using AutoMapper 6.1.1 如何使用 Automapper 函数调用递归地将实体映射到视图模型? - How to recursively map entity to view model with Automapper function call? 自动映射器-如何使用自动映射器将三个实体映射到一个实体? - Automapper - How to map three entity to one with automapper? 自动映射器缺少类型映射配置或不受支持的映射(自动映射器6.1.1) - Automapper missing type map configuration or unsupported mapping (automapper 6.1.1) 如何映射表达式 <func<Entity,DTO> &gt;使用automapper - how to map expression<func<Entity,DTO>> with automapper 如何使用AutoMapper 3和Entity Framework将Integer映射到String - How to map Integer to String with AutoMapper 3 and Entity Framework 如何使用自动映射器在json中映射类实体? - How to map class entity within json with automapper? 实体框架6.1.1禁用模型兼容性检查 - Entity Framework 6.1.1 disable model compatibility checking AutoMapper - map 如何将复杂对象转换为更简单的 model - AutoMapper - How map complex objects to simpler model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM