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에 매핑 중입니다. 모든 속성을 매핑 할 수 있습니다. 하지만 그것은 작동 그리고 재정을자동 매퍼에서 iqueryable 매핑에서 enum에 대한 맵을 만드는 방법은 무엇입니까?
var mapExpr = Mapper.CreateMap<Soure.Employee, destination.Employee>()
.ForMember(x => x.TestEnum, opt => opt.MapFrom(s => (destination.MyEnum2)s.TestEnum));
를 사용하는 경우 열거 매핑에 나에게 INT32 에 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
}
}
을 사용하는 것입니다
AutoMapper를 사용하여 매핑 열거의 2 가지 방법은 ... 다음 이 링크를 확인하십시오 : http://stackoverflow.com/questions/9721466/automapper-cannot-conve rt-enum-to-nullable-int – Mez
그냥지도 만들기 사용하지 마십시오 ??? –
ForMember에서 더 많은 정보를 지정해야하는 이유 as 다른 클래스가 있고 그 사이에 맵을 만들면 직접 매핑됩니다. –