I am using .Net core with Entity Framework. Below is my code
View Model
public class EmployeeVm
{
public int Id { get; set; }
public string Name { get; set; }
public string ContactNo { get; set; }
public string Email { get; set; }
public DateTime JoiningDate { get; set; }
public int BranchId { get; set; }
public int DepartmentId { get; set; }
}
POCO Class
public class employee
{
[Key]
public int id { get; set; }
public string name { get; set; }
public string contact_no { get; set; }
public string email { get; set; }
public DateTime joining_date { get; set; }
public int branch_id { get; set; }
public int department_id { get; set; }
}
Automapper configuration from Startup class
public void ConfigureServices(IServiceCollection services)
{
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
}
Mapping Profile class
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<employee, EmployeeVm>();
CreateMap<EmployeeVm, employee>();
}
}
When I am trying to map View Model properties to POCO class properties using below code it is working fine.
//Here I am using constructor injection
private readonly IMapper _mapper;
public EmployeeBl(IMapper mapper)
{
_mapper = mapper;
}
_mapper.Map<employee>(employeeVm)
But when I am trying to map POCO class (employee) properties to View Module (EmployeeVm) properties then some properties are not mapping as it contains underscore in POCO class
Here is the response of postman
{
"id": 4,
"name": "test",
"contactNo": null,
"email": "[email protected]",
"joiningDate": "0001-01-01T00:00:00",
"branchId": 0,
"departmentId": 0,
}
From above response I am expecting to map contactNo, joiningDate, branchId and departmentId properties with respective value.