title: Automapper cast mapped object to different type
body: <p>I have an interface</p>
  
  <pre><code>public interface ILocation {
      double Longitude { get; set; }
      double Latitude { get; set; }
  }
  </code></pre>
  
  <p>and two classes that inherit from this, both in separate projects (same in both classes)</p>
  
  <pre><code>namespace Mvc {
      [DataContract]
      public class Location : ILocation {
          [DataMember(Name = "longitude")
          public double Longitude { get; set; }
          [DataMember(Latitude = "latitude")
          public double Latitude { get; set; }
      }
  }
  namespace Storage {
      public class Location : ILocation {
          [BsonElement("longitude")
          public double Longitude { get; set; }
          [BsonElement("latitude")
          public double Latitude { get; set; }
      }
  }
  </code></pre>
  
  <p>There is a class that wraps around this ILocation and ILocation is a property of that class e.g.</p>
  
  <pre><code>public interface IVehicle {
      ILocation Location { get; set; }
  }
  
  namespace Mvc {
      [DataContract]
      public class Vehicle : IVehicle {
          [DataMember(Name = "location")
          public ILocation Location { get; set; }
      }
  }
  
  namespace Storage {
      public class Vehicle : IVehiche {
          [BsonElement("location")
          public ILocation Location { get; set; }
      }
  }
  </code></pre>
  
  <p>The class gets passed from one project to another (MVC later to Storage layer) and this is passed via the interface from the MVC layer to the Storage layer.</p>
  
  <p>I am using Automapper to map from the interface to the concrete class so I have access to the attributes when serialising.</p>
  
  <pre><code>MapperConfiguration configuration = new MapperConfiguration(config =&gt; {
      config.CreateMap&lt;IVehicle, Storage.Vehicle&gt;();
      config.CreateMap&lt;ILocation, Storage.Location&gt;();
  };
  Mapper mapper = configuration.CreateMapper();
  </code></pre>
  
  <p>To convert from the interface to the concrete class in the storage project I use </p>
  
  <pre><code>Vehicle vehicle  = mapper.Map&lt;IVehicle, Storage.Vehicle&gt;(data);
  </code></pre>
  
  <p>I then store the data into my storage (mongodb) and it stores the vehicle property as 'location' field name, which is correct. But then the properties of Location are stored as 'Longitude' and 'Latitude' (uppercase first letter) which is incorrect as I have an attribute on both to store as lowercase.</p>
  
  <p>When debugging the vehicle variable and I look at the Location property and it is not of type <code>Storage.Location</code> but of type <code>Mvc.Location</code>. However I have told Automapper to map this to <code>Storage.Location</code>.
  On the Automapper configuration I have tried <code>config.CreateMap&lt;IVehicle, Storage.Vehicle&gt;().As&lt;Storage.Vehicle&gt;;</code> and still no luck.</p>
  
  <p>How do I get Automapper to cast all the nested properties to the correct type specified? I have seen <code>TypeConverters</code> but I have not seen a generic one I can just pass the source and destination types into and it will convert it for me, I seem to have to specify one for each type, which will get quite large in the application I am building.</p>