I am having an issue when using .Include<>. Here is an example:
[TestClass]
public class UnitTest1
{
static UnitTest1()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Base, Target>()
.Include<Derived, Target>();
});
}
[TestMethod]
public void TestMethod1()
{
var src = new Derived
{
X = 5
};
var dest = Mapper.Map<Derived, Target>(src);
Assert.AreEqual(src.X, dest.X);
}
}
class Base
{
public int X { get; set; }
}
class Derived : Base { }
class PolicyBase : Base { }
class Target
{
public int X { get; set; }
public string S { get; set; }
}
This mapping worked successfully from at least 3.3 until the 4.2.0 release. Starting in 4.2.0 (and in 5.0.0), it gives the below exception:
Unable to create instance of class AutoMapperTest.UnitTest1. Error: System.InvalidOperationException: Missing map from Base to Target. Create using Mapper.CreateMap<Base, Target>..
at AutoMapper.QueryableExtensions.Impl.QueryMapperHelper.MissingMapException(Type sourceType, Type destinationType)
at AutoMapper.MapperConfiguration.<GetDerivedTypeMaps>d__89.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at AutoMapper.MapperConfiguration.Seal()
at AutoMapper.MapperConfiguration..ctor(Action`1 configure, IEnumerable`1 mappers, IEnumerable`1 typeMapObjectMappers)
at AutoMapperTest.UnitTest1..cctor() in C:\Development\TestApps\AutoMapperTest\AutoMapperTest\UnitTest1.cs:line 13
I also tested this using the non-static API, but didn't have any luck there either.
I can fix it by adding:
cfg.CreateMap<Derived, Target>();
at initialization, but I thought the purpose of .Include<> was to avoid that.
Any help is much appreciated!
Thanks!
Chris Bristol