我正在尝试使用AutoMapper的ProjectTo扩展方法将基于Entity Framework表的对象转换为自定义DTO。 EF侧的Person实体如下所示:
public partial class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Age { get; set; }
}
我投射的PersonViewModel DTO看起来像这样:
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; }
}
需要注意的关键是person表包含一个名为Age的字段,它是一个nvarchar类型,PersonViewModel包含一个Age类型的int类型。不幸的是,我无法更改表格,必须找到转换数据的方法。
以下代码是我尝试过的AutoMapper配置,它们的问题"以及消耗代码(数据库调用等)。
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();
使用AutoMapper的ProjectTo可以将字符串转换为int吗?其次,如果可能,那个配置是什么样的?
提前感谢您的帮助。
答案 0 :(得分:4)
你的第一次尝试是为什么这不起作用。实体框架不知道如何将字符串转换为int,因此您需要进行软管理。其他方面都不重要,重要的是EF给你的例外。
如果EF无法支持转换,那么AutoMapper无法帮助您。
相反,我会在您的视图模型中执行类似转换的操作:
public class PersonViewModel
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public int AgeValue => Convert.ToInt32(Age);
}
答案 1 :(得分:1)
https://stackoverflow.com/a/24027242/1736944说明了如何向EF添加功能。基本上生成的查询必须执行类型转换。