Reputation: 1766
I have a strange situation that I cannot figure out. I have a simple ASP.NET MVC4 application and am using AutoMapper to move data between ViewModel and Entity (DB) objects.
The issue that I am fighting is that my configuring of AutoMapper in Application_Start()
seems to disappear when I reach Create()
in CustomerController.
If I invoked Configure in the Create()
method, the mapping from CustomerViewModel to CustomerEntity works properly.
I have scoured the Internet and have not seen anyone reporting this issue, so hopefully someone can give me a clue. Here are the details... I hope that someone has some suggestions because it is driving me crazy! :P
In global.asax.cs, in Application_Start()
, I call the configure() method on my class:
AutoMapperConfigurator.Configure();
Here is the class:
public class AutoMapperConfigurator
{
public static void Configure()
{
Mapper.Initialize(x => x.AddProfile<AutoMappingProfile>());
}
}
AutomMappingProfile is my AutoMapper profile class. It is implemented, as follows:
public class AutoMappingProfile : Profile
public override string ProfileName
{
get { return "AutoMappingProfile";}
}
protected override void Configure()
{
CreateMaps();
}
private void CreateMaps()
{
CreateMap<CustomerEntity, CustomerEntity>();
CreateMap<CustomerViewModel, CustomerViewModel>();
CreateMap<CustomerEntity, CustomerViewModel>().IgnoreAllNonExisting();
CreateMap<CustomerViewModel, CustomerEntity>().IgnoreAllNonExisting();
CreateMap<CustomerVendorProductEntity, CustomerVendorProductEntity>();
CreateMap<CustomersVendorsProductViewModel, CustomersVendorsProductViewModel>();
CreateMap<CustomerVendorProductEntity, CustomersVendorsProductViewModel>().IgnoreAllNonExisting();
CreateMap<CustomersVendorsProductViewModel, CustomerVendorProductEntity>().IgnoreAllNonExisting();
}
}
I set a breakpoint on CreateMaps()
and see that it is invoked when the application starts.
When I invoke an action to create a new Customer entity, the Create() method in CustomerController is invoked.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([DataSourceRequest] DataSourceRequest request, CustomerViewModel vm)
{
.
.
var customer = AutoMapper.Mapper.Map<CustomerViewModel, CustomerEntity>(vm);
.
.
}
The mapping always fails with a NotSupportedException
on the mapping from a List to a Collection.
I have implemented ListSourceMapper, which is loaded as a Mapper in Configure(). I don't show it here, because it works... The issue that I am fighting is that my configuring of AutoMapper in Application_Start() seems to disappear when I reach Create() in CustomerController. If I invoked Configure in the Create() method, the mapping from CustomerViewModel to CustomerEntity works properly. I have scoured the Internet and have not seen anyone reporting this issue, so hopefully someone can give me a clue.
Upvotes: 3
Views: 1725
Reputation: 15787
I had a similar issue to this last year, which I described here:
ASP.NET MVC4 AutoMapper: Loses mapping when reach a Controller
I had a controller, which had the following constructor:
IApplicationDAO applicationDAO = new ApplicationDAO();
public ApplicationController()
{
Mapper.Initialize(cfg => {
cfg.CreateMap<ApplicationModel, BusinessObjects.Application>();
});
and a data object, which had the following constructor:
public ApplicationDAO()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<BusinessObjects.Application, DataObjects.dbApplication>()
.ReverseMap();
});
}
The data object is created once and called multiple times by the constructor. Each time the data object is called by the constructor it overwrites the mappings set in ApplicationDAO meaning AutoMapper always produced an error.
The solution for me was to ensure all mappings are created in the composition root i.e. Global.asax.
Upvotes: 0