如何在AutoMapper的IQueryable映射中创建枚举类型的映射?

3
class Program
    {
        static void Main(string[] args)
        {
            var emp1 = new Soure.Employee();
            emp1.TestEnum = Soure.MyEnum1.red;
            var emp2 = new Soure.Employee();
            emp2.TestEnum = Soure.MyEnum1.yellow;
            var empList = new List<Soure.Employee>() { emp1, emp2 }.AsQueryable();
            var mapExpr = Mapper.CreateMap<Soure.Employee, destination.Employee>();
            Mapper.CreateMap<Soure.Department, destination.Department>();
            Mapper.CreateMap<Soure.MyEnum1, destination.MyEnum2>();
            Mapper.AssertConfigurationIsValid();
            var mappedEmp = empList.Project().To<destination.Employee>();

        }  
    }

我正在将Source.Employee映射到Destination.Employee。 所有属性都可以映射。 但是在枚举映射时,它会给我一个异常,无法将TestEnum映射到Int32。 如果使用override,则可以解决问题。

var mapExpr = Mapper.CreateMap<Soure.Employee, destination.Employee>()
        .ForMember(x => x.TestEnum, opt => opt.MapFrom(s => (destination.MyEnum2)s.TestEnum));

那么它就可以工作了。

映射类如下所示:

namespace Soure
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Department dept { get; set; }

        public int age { get; set; }

        public MyEnum1 TestEnum { get; set; }

        public Employee()
        {
            this.Id = 1;
            this.Name = "Test Employee  Name";
            this.age = 10;
            this.dept = new Department();
        }
    }

    public class Department
    {
        public int Id { get; set; }
        public string DeptName { get; set; }

        Employee emp { get; set; }

        public Department()
        {
            Id = 2;
            DeptName = "Test Dept";
        }
    }

    public enum MyEnum1
    {
        red,
        yellow
    }
}

namespace destination
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int age { get; set; }
        public MyEnum2 TestEnum { get; set; }

        public Department dept { get; set; }

    }

    public class Department
    {
        public int Id { get; set; }
        public string DeptName { get; set; }

        Employee emp { get; set; }
    }
    public enum MyEnum2
    {
        red,
        yellow
    }
}

请检查此链接: https://dev59.com/4mkw5IYBdhLWcg3wrsjn - Mez
只使用创建映射,这是否可能? - Hemant Malpote
为什么我们需要在ForMember中指定更多信息?如果我有不同的类并且创建它们之间的映射,它会直接进行映射。 - Hemant Malpote
1个回答

1
以下是使用AutoMapper映射枚举器的两种方法...
.ForMember(x => x.TestEnum, opt => opt.MapFrom(s => (int)s.TestEnum));

另一种方法是使用ConstructUsing

mapper.CreateMap<TestEnum, Soure.MyEnum1>().ConstructUsing(dto => Enumeration.FromValue<Soure.MyEnum1>((int)dto.MyEnum2));

他已经说过了,他不想使用override,即ForMember。 - Hemant Malpote
为什么我们需要在ForMember中指定更多信息?如果我有不同的类并创建它们之间的映射,它会直接进行映射。 - Hemant Malpote
你需要为这些类型创建一个定义,因为它们不是相同的。 - Mez
正如您所看到的,两个枚举的内容是相同的。 - Hemant Malpote
如果你看到 Mapper.CreateMap<Source.Department, destination.Department>(); 这两个是不同的类。但这里不需要使用 ForMember。 - Hemant Malpote

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接