【发布时间】:2018-07-23 18:01:06
【问题描述】:
我目前正在使用 .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; }
}
Dtos
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)
【问题讨论】:
-
哪些属性没有被映射?
-
只有 DropdownValueDto 中的属性字段。返回空值。期望 PropertyDto
-
可能是因为源中的Property为null。
标签: c# .net-core automapper