[英]Another class within the Navigation Properties not getting mapped : AutoMapper
I am trying to map using AutoMapper classes which have navigation properties(EmployeeLeave) which has an instance of another class(Leave).我正在尝试使用具有导航属性(EmployeeLeave)的 AutoMapper 类来 map,该类具有另一个类(Leave)的实例。 I was able to map the navigation property but not the class within the navigation property我能够 map 导航属性,但不是导航属性中的 class
namespace Project.Core.Entities
{
public class ApplicationUser : IdentityUser<int>
{
public string Title { get; set; }
[DisplayName("First Name")]
[Required, MaxLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
public string FirstName { get; set; }
[DisplayName("Middle Name")]
public string MiddleName { get; set; }
[DisplayName("Last Name")]
[Required]
public string LastName { get; set; }
public EmployeePersonalDetails EmployeePersonalDetails { get; set; }
public ICollection<EmployeeLeave> EmployeeLeaves { get; set; }
}
}
The second class is第二个 class 是
namespace Project.Application.Models
{
public class ApplicationUserModel
{
public string Title { get; set; }
[DisplayName("First Name")]
[Required, MaxLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
public string FirstName { get; set; }
[DisplayName("Middle Name")]
public string MiddleName { get; set; }
[DisplayName("Last Name")]
[Required]
public string LastName { get; set; }
public EmployeePersonalDetailsModel EmployeePersonalDetailsModel { get; set; }
public ICollection<EmployeeLeaveModel> EmployeeLeavesModel{ get; set;}
}
}
There is another class(Leave) which is an instance in the EmployeeLeave class还有另一个类(Leave),它是 EmployeeLeave class 中的一个实例
namespace Project.Core.Entities
{
public class EmployeeLeave : Entity
{
public ApplicationUser Employee { get; set; }
public int EmployeeId { get; set; }
public Leave Leave { get; set; }
public int LeaveId { get; set; }
public string LeaveStatus { get; set; }
}
}
This is mapped with这是映射的
namespace Project.Application.Models
{
public class EmployeeLeaveModel : BaseModel
{
public ApplicationUserModel Employee { get; set; }
public int EmployeeId { get; set; }
public LeaveModel LeaveModel { get; set; }
public int LeaveId { get; set; }
public string LeaveStatus { get; set; }
}
}
This is how I am mapping classes这就是我映射类的方式
namespace Project.Application.Mapper
{
public class EmployeeProfile : Profile
{
public EmployeeProfile()
{
CreateMap<Leave, LeaveModel>().ReverseMap();
CreateMap<ApplicationUserModel, ApplicationUser>().ReverseMap()
.ForMember(a =>a.EmployeeLeavesModel , b =>b.MapFrom( b => b.EmployeeLeaves));
CreateMap<EmployeePersonalDetails, EmployeePersonalDetailsModel>().ReverseMap();
CreateMap<EmployeeLeave, EmployeeLeaveModel>().ReverseMap();
}
}
}
This is the method for calculating annual leave taken by the employee where 2 classes are getting mapped这是计算员工年假的方法,其中 2 个班级被映射
public async Task<double> TotalAnnualLeaveTaken(int userId)
{
var employee = await _employeeLeaveRepository.GetEmployee(userId);
var user = _mapper.Map<ApplicationUserModel>(employee);
///rest of the method
}
In the TotalAnnualLeavetaken() method, the employee is getting it value from ApplicationUser class through _employeeLeaveRepository and the navigation property EmployeeLeaves is mapped using 'for member()'.在 TotalAnnualLeavetaken() 方法中,员工通过 _employeeLeaveRepository 从 ApplicationUser class 获取它的值,并且使用“for member()”映射导航属性 EmployeeLeaves。 I want the Leave of the EmployeeLeave class to also map with the LeaveModel of the EmployeeLeaveModel.我希望 EmployeeLeave class 的请假到 map 与 EmployeeLeaveModel 的 LeaveModel。 Is there a way to map it?有没有办法 map 呢?
Your mapping from EmployeeLeave
to EmployeeLeaveModel
does not know what to do with the properties EmployeeLeave.Leave
and EmployeeLeaveModel.LeaveModel
.您从EmployeeLeave
到EmployeeLeaveModel
的映射不知道如何处理EmployeeLeave.Leave
和EmployeeLeaveModel.LeaveModel
属性。 You have to tell him like this:你必须这样告诉他:
CreateMap<EmployeeLeave, EmployeeLeaveModel>()
.ForMember(it => it.LeaveModel, it => it.MapFrom(it2 => it2.Leave));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.