[英]Mapping complex models with Automapper
Here's my original model: 这是我的原始模型:
public class MyClass
{
public string Name{get;set;}
public double Latitude{get;set;}
public double Longitude{get;set;}
public string Street{get;set;}
public string City{get;set;}
public string State{get;set;}
public string Zip{get;set;}
}
And I want to map it to this for JSON serialization purposes: 我想将其映射到此以进行JSON序列化:
public class MyNewClass
{
public string Name{get;set;}
public Location{get;set;}
}
public class Location
{
public string Street{get;set;}
public string City{get;set;}
public string State{get;set;}
public string Zip{get;set;}
public Coordinates Coordinates{get;set;}
}
public class Coordinates
{
public double Latitude{get;set;}
public double Longitude{get;set;}
}
I can't seem to figure out the right way to configure the mapping. 我似乎无法找出配置映射的正确方法。
I was able to finally figure it out. 我终于能够弄清楚了。 Each object needs to be explicity mapped.
每个对象都需要进行显式映射。
Mapper.CreateMap<MyClass, Coordinates>();
Mapper.CreateMap<MyClass, Location>().ForMember(dest => dest.Coordinates, opt => opt.MapFrom(src => src));
Mapper.CreateMap<MyClass, MyNewClass>().ForMember(dest => dest.Location, opt => opt.MapFrom(src => src));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.