我想将我的ASP.NET Core REST Web服务的实体框架模型数据展平到dto。

我的实体类(简化)如下所示:

public class DeliveryNoteEntity
{
    public string VehicleNo { get; set; }
    public int NotMapped { get; set; }
    public List<DeliveryNoteSignature> Signatures { get; set; }
}

public class DeliveryNoteSignature
{
    public string Signature { get; set; }
    public SignedByRole SignedBy { get; set; }
}

public enum SignedByRole
{
    Driver = 1,
    Recipient = 2
}


我的DTO看起来像

public class DeliveryNoteDto
{
    public int Id { get; set; }
    public string VehicleNo { get; set; }
    public string DriverSignature { get; set; }
    public string RecipientSignature { get; set; }
}


然后我可以这样填充我的实体

var sourceEntity = new DeliveryNoteEntity
{
    VehicleNo = "VehicleNo20",
    Signatures = new List<DeliveryNoteSignature> { new DeliveryNoteSignature { Signature = "Driver Mr. Pitz", SignedBy = SignedByRole.Driver} }
};


并映射到dto:

Mapper.Initialize(cfg => cfg.CreateMap<DeliveryNoteEntity, DeliveryNoteDto>()
                                    .ForMember(dest => dest.DriverSignature, opt => opt.MapFrom(src => src.Signatures
                                                                                                          .Where(x => x.SignedBy == SignedByRole.Driver)
                                                                                                          .Select(x => x.Signature)
                                                                                                          .FirstOrDefault()))
                                    .ForMember(dest => dest.RecipientSignature, opt => opt.MapFrom(src => src.Signatures
                                                                                                             .Where(x => x.SignedBy == SignedByRole.Recipient)
                                                                                                             .Select(x => x.Signature)
                                                                                                             .FirstOrDefault()))
                                    .ReverseMap()
                                    );
var dto = Mapper.Map<DeliveryNoteDto>(sourceEntity);


所以我的问题是:当返回到我的服务时,如何从dto到实体进行反向映射?因此,我将手动执行以下操作:

var entityToSave = new DeliveryNoteEntity()
{
    VehicleNo = dto.VehicleNo,
    Signatures = new List<DeliveryNoteSignature>
    {
         new DeliveryNoteSignature {SignedBy = SignedByRole.Driver, Signature = dto.DriverSignature},
         new DeliveryNoteSignature {SignedBy = SignedByRole.Recipient, Signature = dto.RecipientSignature}
    }
};


有没有办法用AutoMapper做到这一点?

编辑:我真正的问题是如何从dto中的XXXSignature属性映射到实体中的列表?

最佳答案

如果使用AutoMapper的命名约定,则ReverseMap也将为您完成。

class CustomerClass
{
    public string Name {get; set;}
    public string Surname {get;set;}
}

class MainClass
{
     public CustomerClass Customer {get; set;}
}




class MainClassDto
{
    public string CustomerName {get; set;}
    public string CustomerSurname {get; set;}

}


对于此示例,您无需为映射和/或反向映射进行任何配置。 AutoMapper使用默认配置处理它

关于c# - AutoMapper-讨人喜欢/不讨人喜欢的列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54554705/

10-15 23:45