title: Automapper Mapping base classes with inheritence
body: <p>I'm having some trouble mapping my parent class using automapper.
  Given the following classes, I have created a mapping profile.  </p>
  
  <p><strong>Mapping classes:</strong></p>
  
  <pre><code>public class SourceClass
  {
      public int SourceProperty1 { get; set; }
      public int SourceProperty2 { get; set; }
      public string SourceProperty3 { get; set; }
      public string SourceProperty4 { get; set; }
  }
  
  public class TargetBaseClass
  {
      public int TargetProperty1 { get; set; }
      public int TargetProperty2 { get; set; }
  }
  
  public class TargetClass1: TargetBaseClass
  {
      public string TargetProperty3 { get; set; }
  }
  
  public class TargetClass2: TargetBaseClass
  {
      public string TargetProperty4 { get; set; }
  }
  </code></pre>
  
  <p><strong>Map:</strong>  </p>
  
  <pre><code>public class MappingProfile: Profile
  {
      protected override void Configure()
      {
          CreateMap&lt;SourceClass, TargetBaseClass&gt;()
              .Include&lt;SourceClass, TargetClass1&gt;()
              .Include&lt;SourceClass, TargetClass2&gt;()
              .ForMember(dst =&gt; dst.TargetProperty1, opt =&gt; opt.MapFrom(src =&gt; src.SourceProperty1))
              .ForMember(dst =&gt; dst.TargetProperty2, opt =&gt; opt.MapFrom(src =&gt; src.SourceProperty2));
          CreateMap&lt;SourceClass, TargetClass1&gt;()
              .ForMember(dst =&gt; dst.TargetProperty3, opt =&gt; opt.MapFrom(src =&gt; src.SourceProperty3));
          CreateMap&lt;SourceClass, TargetClass2&gt;()
              .ForMember(dst =&gt; dst.TargetProperty4, opt =&gt; opt.MapFrom(src =&gt; src.SourceProperty4));
      }
  }
  </code></pre>
  
  <p>And finally <strong>my program:</strong>  </p>
  
  <pre><code>static void Main(string[] args)
  {
      Mapper.Initialize(x =&gt; x.AddProfile&lt;MappingProfile&gt;());
      var sourceClass = new SourceClass
          {
              SourceProperty1 = 1,
              SourceProperty2 = 2,
              SourceProperty3 = "3",
              SourceProperty4 = "4"
          };
      var targetBaseClass = Mapper.Map&lt;TargetBaseClass&gt;(sourceClass);
      var targetClass1 = Mapper.Map&lt;TargetClass1&gt;(sourceClass);
      var targetClass2 = Mapper.Map&lt;TargetClass2&gt;(sourceClass);
  
      Console.WriteLine("TargetBaseClass: {0} {1}", targetBaseClass.TargetProperty1,
                        targetBaseClass.TargetProperty2); //1 2
      Console.WriteLine("TargetClass1: {0} {1} {2}", targetClass1.TargetProperty1, targetClass1.TargetProperty2,
                        targetClass1.TargetProperty3);//0 0 3 ???
      Console.WriteLine("TargetClass2: {0} {1} {2}", targetClass2.TargetProperty1, targetClass2.TargetProperty2,
                        targetClass2.TargetProperty4);//1 2 4
  }
  </code></pre>
  
  <p>The problem is, when I try mapping to the derived classes, my parent class' properties won't get mapped in case of <code>TargetClass1</code> but it <strong>will</strong> for <code>TargetClass2</code>. Can anyone explain to me what it is I'm doing wrong, and why these 2 maps are acting differently? (Does the order in which I <code>Include</code> matter?)</p>
  
  <p><strong>Edit:</strong> On close inspection, the order <em>does</em> indeed matter. However, I still don't know why only the second <code>Include</code> would count.</p>
  
  <p><strong>Edit2:</strong> Based on the comment by @GruffBunny, I think I could "fix" this by using an Extension Method. However, I don't really get why they made it this way. Looking at the code for <code>AutoMapper.TypeMap</code>, I can clearly see this:</p>
  
  <pre><code>public void IncludeDerivedTypes(Type derivedSourceType, Type derivedDestinationType)
  {
      _includedDerivedTypes[derivedSourceType] = derivedDestinationType;
  }
  </code></pre>
  
  <p>Obviously this means you can only specify one destination per included source type. I don't see anything, that would prevent them from supporting more than one destinationtype, however.</p>