I did search in SO + google for mentioned problem, I went through all SO questions but did not get the solution of my issue.
Here is my problem: I am getting AutoMapper.AutoMapperConfigurationException while tried to map IEnumerable prop with List prop.
Here is snippet:
//Logical
public class TestClassPLogical
{
public List<TestClassLogical> TestProp
{
get;
set;
}
}
public class TestClassLogical
{
//defined prop
}
//Model
public class TestClassPDto
{
public virtual IEnumerable<TestClassDto> TestProp
{
get;
set;
}
}
public class TestClassDto
{
//defined prop
}
//Mapping
public class TestClassMapper
{
public static void Configure()
{
//ToDto
Mapper.CreateMap<TestClassPDto, TestClassPLogical>()
.ForMember(d => d.TestProp,
o => o.MapFrom(s => AutoMapperExtensions.MapIEnumerableToList(s.TestProp)));
//ToLogical
Mapper.CreateMap<TestClassPLogical, TestClassPDto>()
.ForMember(d => d.TestProp, o => o.MapFrom(s => AutoMapperExtensions.MapListToIEnumerable(s.TestProp)));
}
}
//Custom class
public class CustomMapperExtensions
{
public static List<T> MapIEnumerableToList<T>(IEnumerable<T> source) where T : class
{
return source == null
? new List<T>()
: source.ToList();
}
public static IEnumerable<T> MapListToIEnumerable<T>(List<T> source) where T : class
{
return source.Count > 0
? source.ToList()
: new List<T>();
}
}
//Unit Test
public class TestClassMapperTest
{
[TestFixtureSetUp]
public void Setup()
{
TestClassMapper.Configure();
}
[Test]
public void EnsureFieldsAreAllMappedOrIgnored()
{
Mapper.AssertConfigurationIsValid();
}
}
P.S.: We are using Fluent Nhibernate as ORM.
Following is the exception:
AutoMapper.AutoMapperConfigurationException : The following property on TestClassPLogical cannot be mapped: TestProp Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type TestClassLogical.
Any help to resolve above issue will be heartily appreciated.
Update I already tried with ReverseMap(), which is throwing same exception, here is snippet:
//Mapping
public class TestClassMapper
{
public static void Configure()
{
//ToDto
Mapper.CreateMap<TestClassPDto, TestClassPLogical>()
.ForMember(d => d.TestProp,
o => o.MapFrom(s => AutoMapperExtensions.MapIEnumerableToList(s.TestProp)));
Mapper.CreateMap<TestClassDto, TestClassLogical>().ReverseMap();
//ToLogical
Mapper.CreateMap<TestClassPLogical, TestClassPDto>()
.ForMember(d => d.TestProp, o => o.MapFrom(s => AutoMapperExtensions.MapListToIEnumerable(s.TestProp)));
Mapper.CreateMap<TestClassLogical, TestClassDto>().Reverse();
}
}
Exception is:
AutoMapper.AutoMapperConfigurationException : The following property on TestClassPLogical cannot be mapped:
TestProp
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type TestClassLogical. Context: Mapping to property TestProp from TestClassDto to TestClassLogical Mapping to property TestProp from System.Collections.Generic.List
1[[TestClassDto, TestMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.List1[[TestClassLogical, TestMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Mapping from type TestClassPDto to ClassPLogical Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown. at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection1 typeMapsChecked, ResolutionContext context) at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection1 typeMapsChecked, ResolutionContext context) at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection1 typeMapsChecked, ResolutionContext context) at AutoMapper.ConfigurationStore.AssertConfigurationIsValid(IEnumerable1 typeMaps) at AutoMapper.ConfigurationStore.AssertConfigurationIsValid() at AutoMapper.Mapper.AssertConfigurationIsValid()