I am trying to map using AutoMapper classes which have navigation properties(EmployeeLeave) which has an instance of another class(Leave). I was able to map the navigation property but not the class within the navigation property
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
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
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
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()'. I want the Leave of the EmployeeLeave class to also map with the LeaveModel of the EmployeeLeaveModel. Is there a way to map it?
Your mapping from EmployeeLeave
to EmployeeLeaveModel
does not know what to do with the properties EmployeeLeave.Leave
and EmployeeLeaveModel.LeaveModel
. You have to tell him like this:
CreateMap<EmployeeLeave, EmployeeLeaveModel>()
.ForMember(it => it.LeaveModel, it => it.MapFrom(it2 => it2.Leave));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.