【发布时间】:2020-07-19 00:04:11
【问题描述】:
根据以下将 Dto 映射到模型 - 映射 = 一个完全空的模型!
在这两个对象中都有可空引用和不可空引用。
模型
public class TheModel
{
public Guid? Id { get; set; }
public Guid Type { get; set; }
public string Title { get; set; } = null!
public string Description { get; set; } = null!;
public string? ItIsAnything { get; set; }
public string Internal { get; set; } = null!;
public string? AnotherInternal { get; set; }
}
Dto
public class TheDto
{
public Guid? Id { get; set; }
public Guid Type { get; set; }
public string? Title { get; set; } // is nullable in the dto
public string Description { get; set; } = null!;
public string? ItIsAnything { get; set; }
}
映射和执行 - Dto 到模型
CreateMap<TheDto, TheModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(dto => dto.Id ?? Guid.Empty))
// attempt for non-nullable references in Model that are nullable in Dto
.ForMember(dest => dest.Internal , opt => opt.NullSubstitute(""));
// execute
var dto = new TheDto{
Id = null,
Type = Guid.NewGuid(),
Title = null;
Description = "any desc";
}
var model = _mapper.Map<TheModel>(dto);
///// model is all empty !
【问题讨论】:
标签: c# automapper c#-8.0 nullable-reference-types