title: AutoMapper.ProjectTo with field level type conversion
body: <p>I am trying to use AutoMapper's ProjectTo extension method to convert an Entity Framework table based object to a custom DTO.  The Person entity on the EF side looks like this:</p>
  
  <pre><code>public partial class Person
  {
      public int PersonId { get; set; }
      public string Name { get; set; }
      public string Age { get; set; }
  }
  </code></pre>
  
  <p>The PersonViewModel DTO that I am projecting to looks like this:</p>
  
  <pre><code>public class PersonViewModel
  {
      public PersonViewModel(Person p)
      {
          PersonId = p.PersonId;
          Name = p.Name;
          Age = Convert.ToInt32(p.Age);
      }
  
      public PersonViewModel()
      {
  
      }
  
      public PersonViewModel(int id, string name, int age)
      {
          PersonId = id;
          Name = name;
          Age = age;
      }
  
      public int PersonId { get; set; }
      public string Name { get; set; }
      public int Age { get; set; }
  }
  </code></pre>
  
  <p>The key thing to note is that the person table contains a field called Age that is a nvarchar type and the PersonViewModel has an int type for Age.  Unfortunately, I cannot change the table and have to find a way to convert the data.</p>
  
  <p>The following code is the AutoMapper configurations I have tried, their "issues", and the consuming code (database call, etc).</p>
  
  <pre><code>MapperConfiguration config = new MapperConfiguration(cfg =&gt; 
          {
              // -----  Throws "Unable to create map expression" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;();
  
  
              // -----  Throws "Linq to Entities does not support method" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ForMember(dest =&gt; dest.Age, opt =&gt; opt.MapFrom(src =&gt; Convert.ToInt32(src.Age)));
  
  
              // -----  Throws "Unable to create map expression" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ConstructProjectionUsing(src =&gt; new PersonViewModel
              //    {
              //        PersonId = src.PersonId,
              //        Name = src.Name,
              //        Age = Convert.ToInt32(src.Age)
              //    });
  
  
              // -----  Throws "Unable to create map expression" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ConstructProjectionUsing(src =&gt; new PersonViewModel(src.PersonId, src.Name, Convert.ToInt32(src.Age)));
  
  
              // -----  Throws "Unable to create map expression" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ConstructProjectionUsing(src =&gt; new PersonViewModel(src.PersonId, src.Name, 25));
  
  
              // -----  Throws "Linq to Entities does not support method" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ProjectUsing(src =&gt; new PersonViewModel
              //    {
              //        PersonId = src.PersonId,
              //        Name = src.Name,
              //        Age = Convert.ToInt32(src.Age)
              //    });
  
  
              // -----  Throws "Unable to create map expression" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ConstructUsing(src =&gt; new PersonViewModel
              //    {
              //        PersonId = src.PersonId,
              //        Name = src.Name,
              //        Age = Convert.ToInt32(src.Age)
              //    });
  
  
              // -----  Throws "Unable to create map expression" exception  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ConstructUsing(src =&gt; new PersonViewModel(src));
  
  
              // -----  Doesn't throw exception, but all values are null or 0.  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ConvertUsing(src =&gt; new PersonViewModel
              //    {
              //        PersonId = src.PersonId,
              //        Name = src.Name,
              //        Age = Convert.ToInt32(src.Age)
              //    });
  
  
              // -----  Doesn't throw exception, but all values are null or 0.  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ConvertUsing(src =&gt; new PersonViewModel(src.PersonId, src.Name, Convert.ToInt32(src.Age)));
  
  
              // -----  Works, but bypasses the conversion I need  ------
              //cfg.CreateMap&lt;Person, PersonViewModel&gt;()
              //    .ProjectUsing(src =&gt; new PersonViewModel
              //    {
              //        PersonId = src.PersonId,
              //        Name = src.Name,
              //        Age = 25
              //    });
  
  
              // -----  Works, but bypasses the conversion I need  ------
              cfg.CreateMap&lt;Person, PersonViewModel&gt;()
                  .ForMember(dest =&gt; dest.Age, opt =&gt; opt.MapFrom(src =&gt; 35));
          });
  
          using (FamilyEntities db = new FamilyEntities())
          {
              PersonViewModel p = db.Set&lt;Person&gt;()
                  .Where(x =&gt; x.PersonId == 1)
                  .ProjectTo&lt;PersonViewModel&gt;(config)
                  .SingleOrDefault();
  
              Console.WriteLine("Name: " + p.Name + Environment.NewLine + "Age: " + p.Age);
          }
  
          Console.ReadLine();
  </code></pre>
  
  <p>Is this conversion of string to int possible using AutoMapper's ProjectTo?   Secondly, if it is possible, what does that configuration look like?</p>
  
  <p>Thanks in advance for any help.</p>