这是我的存储过程,它返回一个Customer_GetCustomers_Result类型:
SELECT c.*, a.City AS AddressCity, a.State AS AddressState
FROM Customer c
LEFT OUTER JOIN Address a ON c.AddressId = a.Id
当使用AutoMapper从Customer_GetCustomers_Result映射到Model.Customer(POCO)时,我希望AddressCity和AddressState映射到Customer.Address.City和Customer.Address.State。
我想使用这个命名约定我不需要为我的AutoMapper配置添加任何特殊内容,除了:
CreateMap<Customer_GetCustomers_Result, Model.Customer>();
我愿意做任何事情来从我的存储过程中获取导航属性。有什么想法吗?
答案 0 :(得分:1)
您可以使用AfterMap
:
Mapper.CreateMap<Customer_GetCustomers_Result, Model.Customer>()
.AfterMap((s,t) =>
{
t.Address = new Address {
City = s.AddressCity,
State = s.AddressState
};
});
甚至可以定义第二个映射......
Mapper.CreateMap<Customer_GetCustomers_Result, Address>()
.ForMember(t => t.City, m => m.MapFrom(s => s.AdressCity))
.ForMember(t => t.State, m => m.MapFrom(s => s.AdressState))
然后......
Mapper.CreateMap<Customer_GetCustomers_Result, Model.Customer>()
.AfterMap((s,t) =>
{
t.Address = Mapper.Map<Address>(s);
});