【发布时间】:2018-06-18 18:31:17
【问题描述】:
public class Restaurant
{
public int RestaurantId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
public bool Active { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
public decimal? Lat { get; set; }
public decimal? Long { get; set; }
}
public class RestaurantInfo
{
public int RestaurantId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
public bool Active { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
public decimal? Lat { get; set; }
public decimal? Long { get; set; }
}
自动映射器
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Restaurant, RestaurantInfo>();
CreateMap<Restaurant, Address>();
}
}
public RestaurantInfo GetRestaurantById(IMapper mapper)
{
var restaurant = new Restaurant
{
RestaurantId = 1,
Name = "Blueline",
Slug = "Blueline",
Active = true,
Address1 = "XYZ",
Address2 = "PQR"
};
return mapper.Map<Restaurant>(restaurantInfo);
}
我的源类是 Restaurant,Destination 类是 RestaurantInfo。自动映射器将 Restaurant 转换回 RestaurantInfo,但问题是 RestaurantInfo 的 Address 属性未使用 Restaurant 的所有地址相关属性进行初始化。我认为我的映射代码不正确。建议我为上述问题正确映射。
【问题讨论】:
-
见this。
标签: c# automapper