[英]AutoMapper one source object to multiple destination objects
I've got a source model defined as 我有一个源模型定义为
public class SourceRoot
{
...
public Organisation Organisation { get; set; }
...
}
public class Organisation
{
public long? Id { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public double SupplementaryAmount { get; set; }
public decimal BaseConversionRate { get; set; }
}
and a destination defined as: 和目的地定义为:
public class DestinationRoot
{
...
public Organisation Organisation { get; set; }
public ContributesTo ContributesTo { get; set; }
}
public class Organisation
{
public long? Id { get; set; }
public string Name { get; set; }
}
public class ContributesTo
{
public string Currency { get; set; }
public double SupplementaryAmount { get; set; }
public decimal BaseConversionRate { get; set; }
}
I want to map from the SourceRoot
to the DestinationRoot
add copy from the source Organisation
to the destination Organisation
AND ContributesTo
. 我想从
SourceRoot
映射到DestinationRoot
添加副本从源Organisation
到目标Organisation
AND ContributesTo
。
I have the following configuration for AutoMapper: 我有AutoMapper的以下配置:
public static class AutoMapperConfig
{
public static MapperConfiguration RegisterMappings()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<MyProfile>();
});
return config;
}
}
public class MyProfile : Profile
{
protected override void Configure()
{
this.CreateMap<SourceRoot, DestinationRoot>();
this.CreateMap<Source.Organisation, Destination.Organisation>();
this.CreateMap<Source.Organisation, Destination.ContributesTo>();
}
}
Using this current profile the Organisation
gets mapped but the ContributesTo
comes out as null. 使用此当前配置文件,
Organisation
将被映射,但ContributesTo
将显示为null。 Note that I'm using version 4.2 of AutoMapper where the static methods have been deprecated so trying to steer away from that. 请注意,我正在使用AutoMapper的4.2版本,其中静态方法已被弃用,因此试图避开它。 Normally I would do:
通常我会这样做:
Mapper.CreateMap<SourceRoot, DestinationRoot>()
.ForMember(d => d.ContributesTo, opt => opt.MapFrom( s=> Mapper.Map<ContributesTo>(s.Organisation)));
But this is not advised anymore (referencing the static methods). 但不建议这样做(引用静态方法)。 Is there an alternative way of doing this?
有没有其他方法可以做到这一点?
Thanks 谢谢
Just add mapping for ContributesTo
destination member: 只需为
ContributesTo
目标成员添加映射:
protected override void Configure ()
{
CreateMap<Source.SourceRoot, Destination.DestinationRoot>()
.ForMember(d => d.ContributesTo, opt => opt.MapFrom(s => s.Organisation));
CreateMap<Source.Organisation, Destination.Organisation>();
CreateMap<Source.Organisation, Destination.ContributesTo>();
}
Otherwise Automapper finds that both source and destinaton roots have property Organisation
and it maps only this property. 否则,Automapper会发现source和destinaton根都具有属性
Organisation
并且它仅映射此属性。 Automapper cannot understand that it should use one property of source to map several properties of destination (which do not match by name). Automapper无法理解它应该使用source的一个属性来映射目标的几个属性(名称不匹配)。 Note that you don't need to specify mapping for
Organisation
member, because it matches property name in destination object. 请注意,您不需要为
Organisation
成员指定映射,因为它与目标对象中的属性名称匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.