0

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.

jamarchist
  • 46
  • 3
  • 1
    There is plenty of answer on this subject. See: http://stackoverflow.com/questions/4052579/automapper-how-to-ignore-property-in-source-item-that-does-not-exist-in-destinat – Simon Belanger Jun 26 '13 at 03:26
  • SO correctly shows this http://stackoverflow.com/questions/16125997/automapper-settings-properties-to-null-on-destination-object as related, but the options I have don't include 'UseDestinationValue'. I'm guessing I have to write a custom IValueResolver or use one of the included resolvers... but which one? – jamarchist Jun 26 '13 at 03:44
  • Yes, I've been sitting in front of the computer too long, mapping back and forth between sources and destinations.... thanks. – jamarchist Jun 26 '13 at 03:49

0 Answers0