My database context has a User
model and a Company
model. Users can be admin, in which case they aren't associated with a company, or they can be one of two other types, in which case they will be associated with a company.
Relevant bits of the two models...
public class User {
public UserRole Role { get; set; }
public int? CompanyId { get; set; }
public virtual Company? Company { get; set; }
}
public class Company {
public virtual ObservableCollection<User> Users { get; set; } = [];
}
I have a corresponding DTO model for the user, and two for the company (one with minimal properties, used for lists, and one with all details used for edit pages). Again, irrelevant bits snipped...
public class UserDto {
public int? CompanyId { get; set; }
public string CompanyName { get; set; } = "";
}
public record CompanyOverviewDto(int Id, bool Active, string Name);
public class CompanyDto {
public List<UserDto> Users { get; set; } = [];
}
I have an AutoMapper profile as follows...
public class AutoMapperProfile : Profile {
public AutoMapperProfile() {
CreateMap<Company, CompanyOverviewDto>();
CreateMap<Company, CompanyDto>().ReverseMap();
CreateMap<User, UserDto>()
.ReverseMap();
}
}
If a UserDto
is passed into AutoMapper, and has a null CompanyId
, then the resulting User
object that it produces has a null CompanyId
, but a non-null Company
object hanging off it.
How can I stop it doing this? If the CompanyId
is null, then I don't want AutoMapper to add an empty company.
Found the answer myself, so posting it here in case it helps anyone.
One of the apparently irrelevant properties in UserDto
was the following...
public string CompanyName { get; set; }
The User
entity had a Company
navigation property, which had a Name
property. One thing (amongst many) that impressed me about AutoMapper was that it picked up on this, and when creating a UserDto
, mapped the company name to CompanyName
.
What I didn't realise was that as CompanyName
is not a nullable string, it came in as an empty string, which caused AutoMapper to assume there should be a Company
object there with an empty Name
. That's why it created one.
I probably could have fixed this by making CompanyName
nullable, but I have a strong dislike of nullable properties where there isn't a really good reason (which admittedly in this case there may have been).
What I did instead was to add an extra rule to the AutoMapper config to ignore this property when creating a User
...
CreateMap<User, UserDto>()
.ForMember(dto => dto.CompanyName, opt => opt.Ignore())
.ReverseMap();
That fixed the problem. Hope this helps someone.