I using automapper in my project as in documentation:
- Configurated it with class:
public class AppMappingProfile : Profile
{
public AppMappingProfile()
{
CreateMap<UserEntity, UserModel>().ReverseMap();
}
}
Enity code:
public record UserEntity(
int Id,
string Name,
string Email,
string PasswordHash,
Roles Roles) : IEntity;
Model code:
public record UserModel(
string Name,
string Email,
string PasswordHash,
Roles Roles = Roles.Default);
-
Added it to services:
services.AddAutoMapper(typeof(AppMappingProfile));
-
I need to use it in method:
public async Task Add(UserModel user)
{
UserEntity userEntity = _mapper.Map<UserModel, UserEntity>(user);
await _dbContext.Users
.AddAsync(userEntity);
await _dbContext.SaveChangesAsync();
}
I debuged it, request data is correct but _mapper.Map() return null
It worked good before, but after some changes (I think i did not changed any logic in this blocks) it broke
I think it might be cause of:
- I started to use records but not classes
- UserModel does not have a property Id
New contributor
zitiret is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.