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:
public partial class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Age { get; set; }
}
The PersonViewModel DTO that I am projecting to looks like this:
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; }
}
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.
The following code is the AutoMapper configurations I have tried, their "issues", and the consuming code (database call, etc).
MapperConfiguration config = new MapperConfiguration(cfg =>
{
// ----- Throws "Unable to create map expression" exception ------
//cfg.CreateMap<Person, PersonViewModel>();
// ----- Throws "Linq to Entities does not support method" exception ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ForMember(dest => dest.Age, opt => opt.MapFrom(src => Convert.ToInt32(src.Age)));
// ----- Throws "Unable to create map expression" exception ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ConstructProjectionUsing(src => new PersonViewModel
// {
// PersonId = src.PersonId,
// Name = src.Name,
// Age = Convert.ToInt32(src.Age)
// });
// ----- Throws "Unable to create map expression" exception ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ConstructProjectionUsing(src => new PersonViewModel(src.PersonId, src.Name, Convert.ToInt32(src.Age)));
// ----- Throws "Unable to create map expression" exception ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ConstructProjectionUsing(src => new PersonViewModel(src.PersonId, src.Name, 25));
// ----- Throws "Linq to Entities does not support method" exception ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ProjectUsing(src => new PersonViewModel
// {
// PersonId = src.PersonId,
// Name = src.Name,
// Age = Convert.ToInt32(src.Age)
// });
// ----- Throws "Unable to create map expression" exception ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ConstructUsing(src => new PersonViewModel
// {
// PersonId = src.PersonId,
// Name = src.Name,
// Age = Convert.ToInt32(src.Age)
// });
// ----- Throws "Unable to create map expression" exception ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ConstructUsing(src => new PersonViewModel(src));
// ----- Doesn't throw exception, but all values are null or 0. ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ConvertUsing(src => 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<Person, PersonViewModel>()
// .ConvertUsing(src => new PersonViewModel(src.PersonId, src.Name, Convert.ToInt32(src.Age)));
// ----- Works, but bypasses the conversion I need ------
//cfg.CreateMap<Person, PersonViewModel>()
// .ProjectUsing(src => new PersonViewModel
// {
// PersonId = src.PersonId,
// Name = src.Name,
// Age = 25
// });
// ----- Works, but bypasses the conversion I need ------
cfg.CreateMap<Person, PersonViewModel>()
.ForMember(dest => dest.Age, opt => opt.MapFrom(src => 35));
});
using (FamilyEntities db = new FamilyEntities())
{
PersonViewModel p = db.Set<Person>()
.Where(x => x.PersonId == 1)
.ProjectTo<PersonViewModel>(config)
.SingleOrDefault();
Console.WriteLine("Name: " + p.Name + Environment.NewLine + "Age: " + p.Age);
}
Console.ReadLine();
Is this conversion of string to int possible using AutoMapper's ProjectTo? Secondly, if it is possible, what does that configuration look like?
Thanks in advance for any help.
https://stackoverflow.com/a/24027242/1736944 explains how to add functionality to EF. Basically the generated query will have to perform the typecast.