c# - 具有字段级类型转换的 AutoMapper.ProjectTo

标签 c# entity-framework entity automapper projection

我正在尝试使用 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?其次,如果可能的话,该配置是什么样的?

在此先感谢您的帮助。

最佳答案

您的第一次尝试是为什么这行不通。 Entity Framework 不知道如何将字符串转换为 int,所以你很吃力。其他方式都不重要,重要的是 EF 给你的异常(exception)。

如果 EF 不支持转换,AutoMapper 将无能为力。

相反,我会在您的 View 模型中执行类似转换的操作:

public class PersonViewModel
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
    public int AgeValue => Convert.ToInt32(Age);
} 

关于c# - 具有字段级类型转换的 AutoMapper.ProjectTo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195363/

相关文章:

c# - List<int> test = {1, 2, 3} - 它是功能还是错误?

c# - 移动跨平台游戏开发的最佳编程语言

c# - SignalR 阻止 chrome UI?

c# - 使实体类因更改而关闭

c# - 为什么我在 Entity Framework 和 SQL Server 事务中得到不同的结果?

php - 如何使用空字符串值将实体保存在数据库中

c# - Entity Framework 、TPT 继承和多语言支持

c# - 我应该在 Model 还是 viewModel 中放置 "required"数据注释?

entity - jhipster 实体字段的默认值

mysql - 共享主机 + Entity Framework + MySQL = 数据提供者错误?