gpt4 book ai didi

mapping - Automapper 复杂映射

转载 作者:行者123 更新时间:2023-12-04 19:15:41 26 4
gpt4 key购买 nike

我如何映射以下内容:

public abstract class PersonBase
{
public Address Address { get; set; }
}

public class Address
{
public string AddressHome { get; set; }
public int PostalNumber { get; set; }
}

public class PersonEditVM : PersonBase
{
public int PersonId { get; set; }
}

public PersonEntity
{
public int PersonId { get; set; }
public string AddressHome { get; set; }
public int PostalNumber { get; set; }
}

我要 map PersonEntityPersonEditVM .

编辑: 一杯咖啡后解决了,但如果它是一个更清洁的解决方案会很好吗?。

Mapper.CreateMap<PersonEntity, PersonEditVM >()                
.ForMember(d=>d.Address, s=>s.MapFrom(p=> new Address{
AddressHome = p.AddressHome,
PostalNumber = p.PostalNumber
}));

最佳答案

这是一种方法,它会 split 您的 Adress (顺便说一下,这是正确拼写的 Address)映射到另一个 CreateMap称呼:

Mapper.CreateMap<PersonEntity, PersonEditVM>()
.ForMember(dest => dest.Adress, opt => opt.MapFrom(src => src));

Mapper.CreateMap<PersonEntity, Adress>()
.ForMember(dest => dest.AdressHome, opt => opt.MapFrom(src => src.AdressHome))
.ForMember(dest => dest.PostalNumber, opt => opt.MapFrom(src => src.PostalNumber));

关于mapping - Automapper 复杂映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com