title: Automapper : mapping issue with inheritance and abstract base class on collections with Entity Framework 4 Proxy Pocos
body: <p>I am having an issue using AutoMapper (which is an excellent technology) to map a business object to a DTO where I have inheritance off of an abstract base class within a collection.</p>
  
  <p>Here are my objects:</p>
  
  <pre><code>abstract class Payment
  class CashPayment : Payment
  class CreditCardPayment : Payment
  </code></pre>
  
  <p>I also have an invoice object which contains a collection of payments like so:</p>
  
  <pre><code>    public class Invoice
      {
         ... properties...
  
         public ICollection&lt;Payment&gt; Payments { get; set; }
      }
  </code></pre>
  
  <p>I also have corresponding DTO versions of each of these objects.</p>
  
  <p>The DtoInvoice object is defined as:</p>
  
  <pre><code>[DataContract]
  public class DtoInvoice
  {
     ...properties...
  
     [DataMember]
     public List&lt;DtoPayment&gt; Payments { get; set; }
  }
  </code></pre>
  
  <p>This is what my Mapper definitions look like:</p>
  
  <pre><code>Mapper.CreateMap&lt;Invoice, DtoInvoice&gt;();
  
  Mapper.CreateMap&lt;Payment, DtoPayment&gt;()
    .Include&lt;CashPayment, DtoCashPayment&gt;()
    .Include&lt;CreditCardPayment, DtoCreditCardPayment&gt;();
  
  Mapper.CreateMap&lt;CashPayment, DtoCashPayment&gt;();
  Mapper.CreateMap&lt;CreditCardPayment, DtoCreditCardPayment&gt;();
  </code></pre>
  
  <p>The code to perform the mapping looks like this:</p>
  
  <pre><code>var invoice = repo.GetInvoice(invoiceId);
  
  var dtoInvoice = Mapper.Map&lt;Invoice, DtoInvoice&gt;(invoice);
  </code></pre>
  
  <p>So for example if my invoice object contains a collection of specific payments (say 1 cash and 1 credit card) when mapper tries to map them I get an error that the abstract class Payment cannot be created. If I remove the abstract keyword from the Payment object then the code works but I only get a collection of Payment object, I do not get their specific objects (Cash &amp; Credit Card payments).</p>
  
  <p>So the question is: How can I get AutoMapper to map the specific payment types and not the base class?</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>I did some more digging and think I see a problem but am not sure how I can solve this with AutoMapper. I think this is more of an EF thing and not AutoMapper's fault. :-)</p>
  
  <p>In my code I am using Entity Framework 4 Proxy POCOs with lazy loading.</p>
  
  <p>So when I try to map an entity returned from EF that is a proxy POCO it gets that funny looking type like:</p>
  
  <pre><code>System.Data.Entity.DynamicProxies.CashPayment_86783D165755C316A2F58A4343EEC4842907C5539AF24F0E64AEF498B15105C2
  </code></pre>
  
  <p>So my theory is that when AutoMapper tries to map CashPayment to DtoCashPayment and the payment passed in is of the proxy type AutoMapper sees it as a "non match" and then maps the generic Payment type. But since Payment is an abstract class AutoMapper bombs with a "System.InvalidOperationException: Instances of abstract classes cannot be created." exception.</p>
  
  <p>So the question is: Is there a way for me to use AutoMapper to map EF POCO proxy objects to Dtos.</p>