AutoMapper v 8.1.1 / EF6
In this example, where Owner.CarId = null, OwnerDto.Car is also null. So if the default behavior is null on a nested property, is there a way to handle the equivalent of .DefaultIfEmpty(new CarDto()) in ProjectTo?
In using a ProjectTo map to convert an optional one-to-one database relationship, if the Owner.CarId is NULL, the CarDto property is null. I thought the default behavior (based on how null collections are handled as I didn't see any mention of how nulls would be evaluated in Nested Mappings) was to return a new CarDto object...and then from there, wasn't sure if the properties of that new CarDto object would be their natural default values (i.e. int = 0) or they'd be null.
Source Classes
public class Car
{
public int Id { get; set; }
public string Model { get; set; }
}
public class Owner
{
public int Id { get; set; }
public Car Car { get; set; }
public int? CarId { get; set; }
public string Name { get; set; }
}
Destination Classes
public class CarDto
{
public int Id { get; set; }
public string Model { get; set; }
}
public class OwnerDto
{
public int Id { get; set; }
public CarDto Car { get; set; }
public int? CarId { get; set; }
public string Name { get; set; }
}
Mapping Configuration
new MapperConfiguration(settings =>
{
settings.CreateMap<Owner, OwnerDto>()
settings.CreateMap<Car, CarDto>();
});