title: Automapper Custom Mapping Exception
body: <p><strong>Update 1-13-10</strong>
  I've been able to find some success using the code below for mapping. I am essentially ignoring any of the properties that do not have a mapping and mapping them afterwards. I would appreciate feedback as to whether or not I am going about this in the best way possible. In addition, I am not sure how to go about unit testing this mapping. It was my impression that using the AutoMapper should help alleviate tediousness of checking each property. </p>
  
  <p>Here is my new code:</p>
  
  <pre><code>Mapper.CreateMap&lt;MoveEntity, MoveEntityDto&gt;()
             .ForMember(dest =&gt; dest.PrimaryOriginTransferee, opt =&gt; opt.Ignore())
             .ForMember(dest =&gt; dest.PrimaryDestinationTransferee, opt =&gt; opt.Ignore())
             .ForMember(dest =&gt; dest.Customer, opt =&gt; opt.Ignore())
             .ForMember(dest =&gt; dest.DestinationAddress, opt =&gt; opt.Ignore())
             .ForMember(dest =&gt; dest.OriginAddress, opt =&gt; opt.Ignore())
             .ForMember(dest =&gt; dest.Order, opt =&gt; opt.Ignore())
             .ForMember(dest =&gt; dest.Shipment, opt =&gt; opt.Ignore())
             .ForMember(dest =&gt; dest.SourceSystemName, opt =&gt; opt.Ignore());
  
          Mapper.CreateMap&lt;ContactEntity, TransfereeEntityDto&gt;();
          Mapper.CreateMap&lt;CustomerEntity, CustomerEntityDto&gt;();
          Mapper.CreateMap&lt;AddressEntity, AddressEntityDto&gt;();
          Mapper.CreateMap&lt;OrderEntity, OrderEntityDto&gt;()
              .ForMember(dest =&gt; dest.OrderForwarding, opt =&gt; opt.Ignore())
              .ForMember(dest =&gt; dest.Forwarder, opt =&gt; opt.Ignore());
          Mapper.CreateMap&lt;ShipmentEntity, ShipmentEntityDto&gt;()
              .ForMember(dest =&gt; dest.Services, opt =&gt; opt.Ignore());
          Mapper.CreateMap&lt;ServiceEntity, ServiceEntityDto&gt;()
              .ForMember(dest =&gt; dest.ServiceTypeCode, opt =&gt; opt.Ignore()) //TODO: ServiceTypeCode not being mapped, should it?
              .ForMember(dest =&gt; dest.SourceSystemName, opt =&gt; opt.MapFrom(src =&gt; Enum.GetName(typeof(SourceSystemName), src.SourceSystemName)));
          Mapper.CreateMap&lt;OrderForwardingEntity, OrderForwardingEntityDto&gt;();
  
  
          Mapper.AssertConfigurationIsValid();
  
  
          MoveEntityDto moveEntityDto = Mapper.Map&lt;MoveEntity, MoveEntityDto&gt;(moveEntity);
          moveEntityDto.PrimaryDestinationTransferee = Mapper.Map&lt;ContactEntity, TransfereeEntityDto&gt;(moveEntity.PrimaryDestinationTransferee);
          moveEntityDto.PrimaryOriginTransferee = Mapper.Map&lt;ContactEntity, TransfereeEntityDto&gt;(moveEntity.PrimaryOriginTransferee);
          moveEntityDto.Customer = Mapper.Map&lt;CustomerEntity, CustomerEntityDto&gt;(moveEntity.Customer);
          moveEntityDto.DestinationAddress = Mapper.Map&lt;AddressEntity, AddressEntityDto&gt;(moveEntity.DestinationAddress);
          moveEntityDto.OriginAddress = Mapper.Map&lt;AddressEntity, AddressEntityDto&gt;(moveEntity.OriginAddress);
          moveEntityDto.Order = Mapper.Map&lt;OrderEntity, OrderEntityDto&gt;(moveEntity.Order);
          moveEntityDto.Order.OrderForwarding = Mapper.Map&lt;OrderForwardingEntity, OrderForwardingEntityDto&gt;(moveEntity.Order.OrderForwarding);
          //moveEntityDto.Order.Forwarder = Mapper.Map&lt;ForwarderEntity, ForwarderEntityDto&gt;(moveEntity.Order.Forwarder);  //Apparently there is no forwarder entity for an Order
          moveEntityDto.Shipment = Mapper.Map&lt;ShipmentEntity, ShipmentEntityDto&gt;(moveEntity.Shipment);
          moveEntityDto.Shipment.Services = Mapper.Map&lt;ServiceEntity[], ServiceEntityDto[]&gt;(moveEntity.Shipment.ServiceEntities);
  </code></pre>
  
  <p><strong>Original Post:</strong></p>
  
  <p>I'm attempting to use AutoMapper for the first time in order to map from a Bussiness Object to a DTO. I am running into issues that I do not know how to troubleshoot, including the following exception:</p>
  
  <blockquote>
    <p>AutoMapper.AutoMapperMappingException: Trying to map Graebel.SP.BO.MoveEntity to Graebel.SOA.Contracts.DataContracts.SP.MoveEntity.
    Exception of type 'AutoMapper.AutoMapperMappingException' was thrown</p>
  </blockquote>
  
  <p>Here is the AutoMapper Code that I am running:</p>
  
  <pre><code>public MoveEntityDto MapMoveEntityToMoveEntityDto(MoveEntity moveEntity)
      {
          Mapper.CreateMap&lt;MoveEntity, MoveEntityDto&gt;()
              .ForMember(dest =&gt; dest.PrimaryOriginTransferee, opt =&gt; opt.MapFrom(src =&gt; src.PrimaryOriginTransferee))
              .ForMember(dest =&gt; dest.PrimaryDestinationTransferee,opt =&gt; opt.MapFrom(src =&gt; src.PrimaryDestinationTransferee))
              .ForMember(dest =&gt; dest.Customer, opt =&gt; opt.MapFrom(src =&gt; src.Customer))
              .ForMember(dest =&gt; dest.DestinationAddress, opt =&gt; opt.MapFrom(src =&gt; src.DestinationAddress))
              .ForMember(dest =&gt; dest.Order, opt =&gt; opt.MapFrom(src =&gt; src.Order))
              .ForMember(dest =&gt; dest.OriginAddress, opt =&gt; opt.MapFrom(src =&gt; src.OriginAddress))
              .ForMember(dest =&gt; dest.Shipment, opt =&gt; opt.MapFrom(src =&gt; src.Shipment))
              .ForMember(dest =&gt; dest.SourceSystemName, opt =&gt; opt.Ignore());
  
          Mapper.AssertConfigurationIsValid();
          MoveEntityDto moveEntityDto = Mapper.Map&lt;MoveEntity, MoveEntityDto&gt;(moveEntity);
  
          return moveEntityDto;
      }
  </code></pre>
  
  <p>Here is the DTO (MoveEntityDto) that I am attempting to map:</p>
  
  <pre><code>public class MoveEntityDto
  {       
      public bool IsOrderDetailPageModified { get; set; }  
      public bool IsRoutingPageModified { get; set; }  
      public bool IsServicePageModified { get; set; }  
      public bool IsContentAndContainerPageModified { get; set; }   
      public string FamilyRange { get; set; }  
      public string Office { get; set; }
      public string ActivityType { get; set; }
      public string ActivitySubject { get; set; }
      public string ActivityNote { get; set; }
      public TransfereeEntity PrimaryOriginTransferee { get; set; }
      public TransfereeEntity PrimaryDestinationTransferee { get; set; }
      public CustomerEntity Customer { get; set; }
      public AddressEntity OriginAddress { get; set; }
      public AddressEntity DestinationAddress { get; set; }
      public OrderEntity Order { get; set; }
      public ShipmentEntity Shipment { get; set; }
      public string PortalId { get; set; }
      public string SourceSystemId { get; set; }
      public EnterpriseEnums.SourceSystemName SourceSystemName { get; set; }
  
      public MoveEntity()
      {
          PrimaryOriginTransferee = new TransfereeEntity();
          PrimaryDestinationTransferee = new TransfereeEntity();
          Customer = new CustomerEntity();
          OriginAddress = new AddressEntity();
          DestinationAddress = new AddressEntity();
          Order = new OrderEntity();
          Shipment = new ShipmentEntity();
      }
  
      public bool HasShipment()
      {
          if (Shipment.ExternalShipmentId &gt; 0)
          {
              return true;
          }
          return false;
      }
   }
  </code></pre>
  
  <p>Here is the Business Object (MoveEntity) that I am trying to map from</p>
  
  <pre><code> public class MoveEntity
  {
      public int SourceId { get; set; }
      public int MoveId { get; set; }
      public bool IsOrderDetailPageModified { get; set; }  // TODO: Internal -  Remove from data contract
      public bool IsRoutingPageModified { get; set; }  // TODO: Internal -  Remove from data contract
      public bool IsServicePageModified { get; set; }  // TODO: Internal -  Remove from data contract
      public bool IsContentAndContainerPageModified { get; set; }  // Rmove from data contract
      public string FamilyRange { get; set; } // TODO: Is this being used?
      public string Office { get; set; }
      public string ActivityType { get; set; }
      public string ActivitySubject { get; set; }
      public string ActivityNote { get; set; }
      public ContactEntity PrimaryOriginTransferee { get; set; }
      public ContactEntity PrimaryDestinationTransferee { get; set; }
      public CustomerEntity Customer { get; set; }
      public AddressEntity OriginAddress { get; set; }
      public AddressEntity DestinationAddress { get; set; }
      public OrderEntity Order { get; set; }
      public ShipmentEntity Shipment { get; set; }
      public string CreatedBy { get; set; }
      public DateTime CreatedDate { get; set; }
      public string ModifiedBy { get; set; }
      public DateTime ModifiedDate { get; set; }
      public string SourceSystemId { get; set; }
      public string SourceSystemName { get; set; }
      public string Version { get; set; }
      public string PortalId { get; set; }
  
      public MoveEntity()
      {
          PrimaryOriginTransferee = new ContactEntity
          {
              ContactTypeId = ContactEntity.ContactType.PrimaryOriginationTransferee
          };
  
          PrimaryDestinationTransferee = new ContactEntity
          {
              ContactTypeId = ContactEntity.ContactType.PrimaryDestinationTransferee
          };
  
          OriginAddress = new AddressEntity
          {
              AddressTypeId = AddressEntity.AddressType.Origination
          };
  
          DestinationAddress = new AddressEntity
          {
              AddressTypeId = AddressEntity.AddressType.Destination
          };
  
          Order = new OrderEntity();
          Customer = new CustomerEntity();
          Shipment = new ShipmentEntity();
      }
  
      public bool HasShipment()
      {
          if (Shipment.ShipmentId &gt; 0)
          {
              return true;
          }
          return false;
      }
  }
  </code></pre>
  
  <p>The properties within each class almost match up perfectly by name, but their types are different. Therefore I have attempted to perform a custom mapping using the "MapFrom" expression. However, AutoMapper doesn't seem to be able to allow me to point from one object type to another without complain.</p>
  
  <p>I've also tried mapping property-to-property, with no luck. It looked something like this:</p>
  
  <pre><code>.ForMember(dest =&gt; dest.PrimaryOriginTransferee.Email, opt =&gt; opt.MapFrom(src =&gt; src.PrimaryOriginTransferee.Email))
  </code></pre>
  
  <p>However, when attempting this, I receive the following exeception:</p>
  
  <blockquote>
    <p>must resolve to top-level member.
    Parameter name: lambdaExpression.</p>
  </blockquote>
  
  <p>I have been finding the documentation available for AutoMapper difficult to follow. Can someone please point me in the right direction as to how to use this utility correctly?</p>
  
  <p>Thanks in advance for any help!</p>
  
  <p>Adam</p>