Automapper not setting navigation property values from stored procedure

automapper c# entity-framework entity-framework-6


My Customer.Model has a navigation property:

public Address Address { get; set; }

I have a stored procedure that has a select that looks like the following:

     a.City AS AddressCity, 
     a.State AS AddressState

These values end up in a complex object that EF6 auto generates:


In my AutoMapper config I have:

CreateMap<Customer_GetCustomers_Result, Model.Customer>();

In my Repository I have:

public IEnumerable<Model.Customer> GetCustomers()
    var cList = context.Customer_GetCustomers();
    return Mapper.Map<List<Model.Customer>>(cList);

cList ends up being of type Customer_GetCustomers_Result so AutoMapper converts it to Model.Customer and returns so my app can use it.

I was told AutoMapper will automatically set Customer.Address.City and Customer.Address.State if I followed that naming convention. What am I missing?

2/21/2015 9:33:40 AM

Popular Answer

Your referring to what's called flattening. But it's the other way around. AutoMapper can map Customer.Address.City to a property Target.AddressCity, but not the reverse. It would have to create an Address object and only set its City property, and then keep in mind that for the second mapping the same Address should be used.

Just too many intricacies and edge cases to get involved in for a tool that's supposed to do a straightforward job.

2/21/2015 12:19:37 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow