我有一个班级员工:
public partial class Employee
{
public Employee()
{
this.Employees1 = new HashSet<Employee>();
}
public int Id { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public Nullable<int> ReportsToId { get; set; }
public virtual ICollection<Employee> Employees1 { get; set; }
public virtual Employee ReportsTo { get; set; }
}
还有一个视图模型:
public class EmployeeEditModel : MappedViewModel<Employee>
{
public int Id { get; set; }
public string Surname { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
public int? ReportsTo { get; set; }
}
MappedViewModel<T>
声明一个方法:
public class MappedViewModel<TEntity>: ViewModel
{
public virtual TEntity MapToEntity()
{
return (TEntity)Mapper.Map(this, GetType(), typeof(TEntity));
}
}
当我调用MapToEntity
一个动作方法时,像这样
我从 AutoMapper 收到异常消息:
缺少类型映射配置或不支持的映射。
映射类型:Int32 -> Employee System.Int32 -> Leave.Data.Employee
目标路径:Employee.ReportsTo.ReportsTo
源值:5
我的映射定义如下:
public static void RegisterMaps()
{
Mapper.CreateMap<Employee, EmployeeCreateModel>();
Mapper.CreateMap<EmployeeCreateModel, Employee>();
}
双重引用Employee.ReportsTo.ReportsTo
告诉我这个异常是由某种循环映射怪癖引起的,但我能做些什么呢?至少我想ReportsTo
从映射中省略,只需在调用MapToEntity
. 我怎么能做到这一点,更确切地说,我应该为这种 AutoMapper 问题模式做些什么?