I'm having an issue w/ AutoMapper (3.3.1). I have two queries, one returns a list of an Entity Framework object, the other returns a DataTable. The reason for this is we need different data/results returned from these queries. I'd like to map both of these to the same object model, but I am experiencing strange behavior. Depending on which route I go (see below), I either get an AutoMapper Exception, or I get no values in my list at all. We have multiple objects that do this and would be nice to know what the correct setup is.
Scenario 1
// this function runs on application_start
public static void ConfigureMapping() {
// ...
AutoMapper.Mapper.CreateMap<SubObjectClassModel, LUSubObjectClass>();
AutoMapper.Mapper.CreateMap<LUSubObjectClass, SubObjectClassModel>()
.ForMember(x => x.ObjectClassCode,
y => y.MapFrom(x => x.LUObjectClass.Code))
.ForMember(x => x.ObjectClassGroupCode,
y => y.MapFrom(x => x.LUObjectClass.GroupCode));
AutoMapper.Mapper.CreateMap<IDataReader, SubObjectClassModel>();
}
public static List<SubObjectClassModel> Gets()
{
LUSubObjectClass subObjectClass = new LUSubObjectClass();
return Mapper.Map<IDataReader, List<SubObjectClassModel>>(
subObjectClass.Gets().CreateDataReader()
);
}
Running this on the server always gives me this exception:
Missing type map configuration or unsupported mapping. Mapping types: IDataReader -> List`1 System.Data.IDataReader -> System.Collections.Generic.List`1[[BRS.Models.Common.SubObjectClassModel, BRS.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Destination path: List`1 Source value: System.Data.DataTableReader
Scenario 2 I tried the solution outlined in this post: http://www.geekytidbits.com/automapper-with-datatables/ but when I run this on the server, I get an empty list back.
With this scenario, I do not create this mapping in my ConfigureMapping fx, just the first:
AutoMapper.Mapper.CreateMap<IDataReader, SubObjectClassModel>();
I've also tried this:
AutoMapper.Mapper.CreateMap<IDataReader, List<SubObjectClassModel>>();
What is the correct way to go about this?
EDIT Here is some weird behavior. If I do not rebuild my entire project (specifically the project that has the Configure call), I either get the automap exception or no results. If I rebuild everything, I get results. Then when I deploy to the server, I get either the exception or no results.
EDIT 2
Upon further investigation I noticed the following. When I do a run from VS without rebuilding the entire solution and I see no results being returned after the Mapper.Map call, I see all the mappings listed correctly. However, Automappers.Mapper.DataReaderMapper is not listed under AutoMapper.ConfigurationStore.
When results are returned successfully, usually after I rebuild the solution, this mapper is listed.
What would cause that behavior and how can I ensure the DataReaderMapper gets loaded every time?