无法在 AutoMapper 中配置 ReverseMap
Can't configure ReverseMap in AutoMapper
我是 AutoMapper 新手。我的映射没有按预期工作,我确定我做错了什么但无法弄清楚。抱歉,如果这个问题令人困惑,但我会尽力弄清楚。假设我们有三个 类:
public class Person
{
public ContactInfo1 Contact { get; set; }
}
public class ContactInfo1
{
public string Name { get; set; }
}
public class ContactInfo2
{
public string AnotherName { get; set; }
}
现在,我想设置我的映射,以便 ContactInfo1 可以映射到 ContactInfo2 或从 ContactInfo2 映射。然后我希望能够映射 Person1 -> ContactInfo2(这可能看起来很奇怪,但无论如何我都需要这样做)。我尝试了以下映射配置:
var autoMapperConfig = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<ContactInfo1, ContactInfo2>()
.ForMember(dest => dest.AnotherName, opt => opt.MapFrom(src => src.Name)).ReverseMap();
cfg.CreateMap<ContactInfo2, Person>()
.ForMember(dest => dest.Contact, opt => opt.MapFrom(src => src)).ReverseMap();
});
var mapper = autoMapperConfig.CreateMapper();
对于测试数据:
var testPerson = new Person();
testPerson.Contact = new ContactInfo1() { Name = "Person1" };
我执行以下操作:
var contactInfo2Test = mapper.Map<Person, ContactInfo2>(testPerson);
这不会给我任何错误,但是 contactInfo2Test.AnotherName 是空的。请指教!谢谢。
Please note that I realize I could go:
cfg.CreateMap<Person, ContactInfo2>()
.ForMember(dest => dest.AnotherName, opt => opt.MapFrom(src => src.Contact.Name));
Bu then I would have mapped Contact1->Contact2 all over again, and in a more complex scenario I really want to avoid that.
这是一种方法:
var autoMapperConfig = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<ContactInfo1, ContactInfo2>()
.ForMember(dest => dest.AnotherName, opt => opt.MapFrom(src => src.Name))
.ReverseMap();
cfg.CreateMap<Person, ContactInfo2>()
.ConstructUsing((p, ctx) => ctx.Mapper.Map<ContactInfo2>(p.Contact));
});
我是 AutoMapper 新手。我的映射没有按预期工作,我确定我做错了什么但无法弄清楚。抱歉,如果这个问题令人困惑,但我会尽力弄清楚。假设我们有三个 类:
public class Person
{
public ContactInfo1 Contact { get; set; }
}
public class ContactInfo1
{
public string Name { get; set; }
}
public class ContactInfo2
{
public string AnotherName { get; set; }
}
现在,我想设置我的映射,以便 ContactInfo1 可以映射到 ContactInfo2 或从 ContactInfo2 映射。然后我希望能够映射 Person1 -> ContactInfo2(这可能看起来很奇怪,但无论如何我都需要这样做)。我尝试了以下映射配置:
var autoMapperConfig = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<ContactInfo1, ContactInfo2>()
.ForMember(dest => dest.AnotherName, opt => opt.MapFrom(src => src.Name)).ReverseMap();
cfg.CreateMap<ContactInfo2, Person>()
.ForMember(dest => dest.Contact, opt => opt.MapFrom(src => src)).ReverseMap();
});
var mapper = autoMapperConfig.CreateMapper();
对于测试数据:
var testPerson = new Person();
testPerson.Contact = new ContactInfo1() { Name = "Person1" };
我执行以下操作:
var contactInfo2Test = mapper.Map<Person, ContactInfo2>(testPerson);
这不会给我任何错误,但是 contactInfo2Test.AnotherName 是空的。请指教!谢谢。
Please note that I realize I could go:
cfg.CreateMap<Person, ContactInfo2>() .ForMember(dest => dest.AnotherName, opt => opt.MapFrom(src => src.Contact.Name));
Bu then I would have mapped Contact1->Contact2 all over again, and in a more complex scenario I really want to avoid that.
这是一种方法:
var autoMapperConfig = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<ContactInfo1, ContactInfo2>()
.ForMember(dest => dest.AnotherName, opt => opt.MapFrom(src => src.Name))
.ReverseMap();
cfg.CreateMap<Person, ContactInfo2>()
.ConstructUsing((p, ctx) => ctx.Mapper.Map<ContactInfo2>(p.Contact));
});