导航属性中的另一个 class 未映射:AutoMapper
Another class within the Navigation Properties not getting mapped : AutoMapper
我正在尝试使用具有导航属性 (EmployeeLeave) 的 AutoMapper classes 进行映射,该属性具有另一个 class(Leave) 的实例。我能够映射导航 属性 但不能映射导航 属性
中的 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; }
}
}
第二个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;}
}
}
还有一个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; }
}
}
这是映射到
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; }
}
}
这就是我映射的方式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();
}
}
}
这是计算员工年假的方法,其中 2 classes 正在映射
public async Task<double> TotalAnnualLeaveTaken(int userId)
{
var employee = await _employeeLeaveRepository.GetEmployee(userId);
var user = _mapper.Map<ApplicationUserModel>(employee);
///rest of the method
}
在 TotalAnnualLeavetaken() 方法中,员工通过 _employeeLeaveRepository 从 ApplicationUser class 获取它的值,并且使用 'for member()' 映射导航 属性 EmployeeLeaves。我希望 EmployeeLeave class 的 Leave 也与 EmployeeLeaveModel 的 LeaveModel 映射。有没有办法映射它?
您从 EmployeeLeave
到 EmployeeLeaveModel
的映射不知道如何处理属性 EmployeeLeave.Leave
和 EmployeeLeaveModel.LeaveModel
。你必须这样告诉他:
CreateMap<EmployeeLeave, EmployeeLeaveModel>()
.ForMember(it => it.LeaveModel, it => it.MapFrom(it2 => it2.Leave));
我正在尝试使用具有导航属性 (EmployeeLeave) 的 AutoMapper classes 进行映射,该属性具有另一个 class(Leave) 的实例。我能够映射导航 属性 但不能映射导航 属性
中的 classnamespace 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; }
}
}
第二个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;}
}
}
还有一个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; }
}
}
这是映射到
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; }
}
}
这就是我映射的方式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();
}
}
}
这是计算员工年假的方法,其中 2 classes 正在映射
public async Task<double> TotalAnnualLeaveTaken(int userId)
{
var employee = await _employeeLeaveRepository.GetEmployee(userId);
var user = _mapper.Map<ApplicationUserModel>(employee);
///rest of the method
}
在 TotalAnnualLeavetaken() 方法中,员工通过 _employeeLeaveRepository 从 ApplicationUser class 获取它的值,并且使用 'for member()' 映射导航 属性 EmployeeLeaves。我希望 EmployeeLeave class 的 Leave 也与 EmployeeLeaveModel 的 LeaveModel 映射。有没有办法映射它?
您从 EmployeeLeave
到 EmployeeLeaveModel
的映射不知道如何处理属性 EmployeeLeave.Leave
和 EmployeeLeaveModel.LeaveModel
。你必须这样告诉他:
CreateMap<EmployeeLeave, EmployeeLeaveModel>()
.ForMember(it => it.LeaveModel, it => it.MapFrom(it2 => it2.Leave));