猿问

Automapper 不适用于嵌套对象

我目前使用 .net core 2.1 并尝试对嵌套对象使用 automapper 将模型转换为 dto 并将 dto 转换为模型。当每个字段都被正确映射时,关系映射就会出现问题。


楷模


public class DropdownValue

{

    public int Id { get; set; }

    public string Value { get; set; }

    public int PropertyId { get; set; }

    public Property Property { get; set; }

}


public class Property

{

    public int Id { get; set; }

    public string Title { get; set; }

    public ValueTypes ValueType { get; set; }

    public InputTypes InputType { get; set; }

    public List<DropdownValue> DropdownValues { get; set; }

}

托斯


public class DropdownValueDto

{

    public int Id { get; set; }

    public string Value { get; set; }

    public PropertyDto Property { get; set; }

}


public class PropertyDto

{

    public int Id { get; set; }

    public string Title { get; set; }

    public InputTypes InputType { get; set; }

    public ValueTypes ValueType { get; set; }

}

映射器


public class MappingProfile : Profile

{

    public MappingProfile() 

    {

        CreateMap<Property, PropertyDto>();

        CreateMap<DropdownValue, DropdownValueDto>();

    }

}

在处理程序中的使用


_mapper.Map<List<Models.DropdownValue>, List<DropdownValueDto>>(dropdownValues)


元芳怎么了
浏览 273回答 2
2回答

12345678_0001

//Modelspublic class DropdownValue{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Value { get; set; }&nbsp; &nbsp; public int PropertyId { get; set; }&nbsp; &nbsp; public Property Property { get; set; } = new Property();}public class Property{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Title { get; set; }&nbsp; &nbsp; public ValueTypes ValueType { get; set; } = new ValueTypes();&nbsp; &nbsp; public InputTypes InputType { get; set; } = new InputTypes();&nbsp; &nbsp; public List<DropdownValue> DropdownValues { get; set; } = new List<DropdownValue>();}//Dtospublic class DropdownValueDto{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Value { get; set; }&nbsp; &nbsp; public PropertyDto Property { get; set; } = new PropertyDto();}public class PropertyDto{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Title { get; set; }&nbsp; &nbsp; public InputTypes InputType { get; set; } = new InputTypes();&nbsp; &nbsp; public ValueTypes ValueType { get; set; } = new ValueTypes();}

婷婷同学_

我总是在 .net 4x 框架项目中使用automapper映射工具,但是当我开发 .net 核心项目时,我总是使用并推荐mapster映射工具。它非常快速和简单!基准测试结果它还可以解决您的问题。您可以在下面查看示例用法。首先创建一个映射器类。public static class Mapper{&nbsp; &nbsp; public static void CreateMap()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; TypeAdapterConfig<Property, PropertyDto>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .NewConfig();&nbsp; &nbsp; &nbsp; &nbsp; TypeAdapterConfig<DropdownValue, DropdownValueDto>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .NewConfig();&nbsp; &nbsp; }}启动时初始化&nbsp; &nbsp; public Startup(IHostingEnvironment env)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // other stuffs&nbsp; &nbsp; &nbsp; &nbsp; // Mapping&nbsp; &nbsp; &nbsp; &nbsp; Mapper.CreateMap();&nbsp; &nbsp; }用法dropdownValues.Adapt<List<Models.DropdownValue>, List<DropdownValueDto>>()
随时随地看视频慕课网APP
我要回答