I was coding a web API, wanted to map User model to UserView model. Debugging confirms mapper.Map(user) returns null value object. mapper is an instance of IMapper Class of AutoMapper.
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public UserRole? Role { get; set; }
}
public class UserView
{
public Guid Id { get; }
public string Name { get; }
public string Email { get; }
public UserRole? Role { get; }
}
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<User, UserView>();
}
}
//In startup.cs
services.AddAutoMapper();
//In user service class.
var userView = _mapper.Map<UserView>(user);
The output looks like this.
{
"id": "00000000-0000-0000-0000-000000000000",
"name": null,
"email": null,
"role": null
}
UserViewhas only property getters, how do you expect them to be populated.