我的Customer.Model
有一个导航属性:
public Address Address { get; set; }
我有一个存储过程,其选择如下所示:
SELECT
c.*,
a.City AS AddressCity,
a.State AS AddressState
这些值最终出现在EF6自动生成的复杂对象中:
Customer_GetCustomers_Result
在我的AutoMapper配置中,我有:
CreateMap<Customer_GetCustomers_Result, Model.Customer>();
在我的存储库中,我有:
public IEnumerable<Model.Customer> GetCustomers()
{
var cList = context.Customer_GetCustomers();
return Mapper.Map<List<Model.Customer>>(cList);
}
cList
最终属于Customer_GetCustomers_Result
类型,因此AutoMapper会将其转换为Model.Customer
并返回,以便我的应用可以使用它。
有人告诉我,如果遵循该命名约定,AutoMapper将自动设置Customer.Address.City
和Customer.Address.State
。我错过了什么?
答案 0 :(得分:0)
您提及的是flattening。但事实恰恰相反。 AutoMapper可以将Customer.Address.City
映射到属性Target.AddressCity
,但不能反过来。它必须创建一个Address
对象并仅设置其City
属性,然后记住,对于第二个映射,应使用相同的Address
。
过多的错综复杂和边缘情况需要参与一个应该做一个简单工作的工具。