I've found what is either a bug with AutoMapper or some convention-based behavior that I need to override. The code below reproduces the issue. How do I keep AutoMapper from trying to write to the FooType
property in the scenario below?
[Test]
public void FooTypeShouldNotBeAltered()
{
AutoMapper.Mapper.CreateMap(typeof (TypeB), typeof (TypeA))
.ForMember("Foo", opt => opt.MapFrom("Foo"));
var typeA = new TypeA { FooType = "foo" };
var typeB = new TypeB { Foo = "bar" };
AutoMapper.Mapper.Map(typeB, typeA, typeof (TypeB), typeof (TypeA));
Assert.AreEqual("foo", typeA.FooType);
}
public class TypeA
{
public string Foo { get; set; }
public string FooType { get; set; }
}
public class TypeB
{
public string Foo { get; set; }
}
Here is the output of the test:
Expected string length 3 but was 13. Strings differ at index 0.
Expected: "foo"
But was: "System.String"
-----------^
(For what it's worth, it's clear to me (though not necessarily completely demonstrated by this code) that AutoMapper is setting the value of the FooType
property to the typename of the Foo
property.)
For now I'm looking for a way to stop this from happening without using generics-based overloads or custom resolvers, and I need to use the Map(source, dest, sourceType, destType)
overload to execute the actual mapping.