映射子集合
Mapping child collections
我正在使用自动映射器将视图模型映射到 dto。下面是一些测试示例。
public class Vehicle
{
public Vehicle()
{
AboutYou = new AboutYou();
TestModels = new List<TestModel>();
}
public AboutYou AboutYou { get; set; }
public List<TestModel> TestModels { get; set; }
}
public class AboutYou
{
[DisplayName("What is your title")]
public int Title { get; set; }
}
public class TestModel
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
和Dto。
public class TestModel
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
public class VehicleDto
{
public VehicleDto()
{
TestDtos = new List<TestDto>();
}
public int AboutYouTitle { get; set; }
public List<TestDto> TestDtos { get; set; }
}
public class TestDto
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
这是我的个人资料。
public class VehicleMapping : Profile
{
public VehicleMapping()
{
CreateMap<Vehicle, VehicleDto>().ReverseMap();
CreateMap<TestModel, TestDto>().ReverseMap();
}
}
我遇到的问题是,我需要将 reversemap
放在适当的位置,以便它使 AboutYou class 变平和变平。但是,TestModels 集合未映射到 TestDtos 集合。我可以应用 AfterMap
但我必须手动展开 AboutYou 对象。我有什么想法可以设置它,以便 AboutYou 被展平和展平并且集合也被映射?
明确指定如何映射成员对我有用
public class VehicleMapping : Profile
{
public VehicleMapping()
{
CreateMap<Vehicle, VehicleDto>()
.ForMember(x => x.TestDtos, opt => opt.MapFrom(src => src.TestModels))
.ReverseMap();
CreateMap<TestModel, TestDto>().ReverseMap();
}
}
我正在使用自动映射器将视图模型映射到 dto。下面是一些测试示例。
public class Vehicle
{
public Vehicle()
{
AboutYou = new AboutYou();
TestModels = new List<TestModel>();
}
public AboutYou AboutYou { get; set; }
public List<TestModel> TestModels { get; set; }
}
public class AboutYou
{
[DisplayName("What is your title")]
public int Title { get; set; }
}
public class TestModel
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
和Dto。
public class TestModel
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
public class VehicleDto
{
public VehicleDto()
{
TestDtos = new List<TestDto>();
}
public int AboutYouTitle { get; set; }
public List<TestDto> TestDtos { get; set; }
}
public class TestDto
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
这是我的个人资料。
public class VehicleMapping : Profile
{
public VehicleMapping()
{
CreateMap<Vehicle, VehicleDto>().ReverseMap();
CreateMap<TestModel, TestDto>().ReverseMap();
}
}
我遇到的问题是,我需要将 reversemap
放在适当的位置,以便它使 AboutYou class 变平和变平。但是,TestModels 集合未映射到 TestDtos 集合。我可以应用 AfterMap
但我必须手动展开 AboutYou 对象。我有什么想法可以设置它,以便 AboutYou 被展平和展平并且集合也被映射?
明确指定如何映射成员对我有用
public class VehicleMapping : Profile
{
public VehicleMapping()
{
CreateMap<Vehicle, VehicleDto>()
.ForMember(x => x.TestDtos, opt => opt.MapFrom(src => src.TestModels))
.ReverseMap();
CreateMap<TestModel, TestDto>().ReverseMap();
}
}