如何使用 AutoMapper 映射两个对象集合

How can use AutoMapper to map two collections of objects

我尝试使用 AutoMapper,但是当我映射 2 集合时出现错误。 这些是我要使用 AutoMapper 的实体和 dto 类:

public class HeadQuarters
{
    public int Id { get; private set; }
    public string HeadQuartersName { get; set; }
    public string HeadQuartersCode { get; set; }
    public string HeadQuartersDescription { get; set; }        
    public bool IsActiv { get; set; }
    public ICollection<Adresa> Adresa { get; set; }      
}

public class HeadQuartersDTO
{
    public string HeadQuartersName { get; set; }
    public string HeadQuartersCode { get; set; }
    public string HeadQuartersDescription { get; set; }
    public ICollection<AdresaDTO> Addresses { get; set; }        
    public EntityState Status { get; set; }
}

这些是我的实体和 dto 集合 类:

public class AdresaDTO
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string StreetNr { get; set; }
    public string Block { get; set; }
    public string Entrance{ get; set; }
    public string Apartment{ get; set; }
    public double? Longitude { get; set; }
    public double? Latitude { get; set; }
    public int? CityId { get; set; }      
    public EntityState Status { get; set; }
}

public partial class Adresa
{
    public int Id { get; private set; }
    public string Street { get; set; }
    public string StreetNr { get; set; }
    public string Block { get; set; }
    public string Entrance{ get; set; }
    public string Apartment{ get; set; }
    public double? Longitude { get; set; }
    public double? Latitude { get; set; }
    public int CityId { get; set; }
    public int? HeadQuartersId { get; set; }
    public int? EmployeeId { get; set; }
    public int? ContractPersonDataId { get; set; }
}

我写这段代码是为了使用 AutoMapper:

public static HeadQuarters DtoToEntity(HeadQuartersDTO dto)
{
    var mapper = new Mapper(MapperConfiguration());            
    return  mapper.Map<HeadQuarters>(dto);            
}

private static MapperConfiguration MapperConfiguration()
{
    return new MapperConfiguration(cfg =>
        cfg.CreateMap<HeadQuartersDTO, HeadQuarters>()
        .ForMember(dest => dest.Adresa, act => act.MapFrom(src => src.Addresses)));
}

但是当我在集合中添加一些时出现错误。这是我收到的错误消息:

如果我明白你想做什么,你应该能够像这样更新你的代码:

public static HeadQuarters DtoToEntity(HeadQuartersDTO dto)
{
    var mapper = new Mapper(MapperConfiguration());            
    return  mapper.Map<HeadQuarters>(dto);            
}

private static MapperConfiguration MapperConfiguration()
{
    return new MapperConfiguration(cfg =>
        cfg.CreateMap<AdresaDTO, Adresa>()
            .ForMember(dest => dest.CityId, act => act.MapFrom(src => src.CityId ?? default(int)))
            .ForMember(dest => dest.HeadQuartersId, act => act.Ignore())
            .ForMember(dest => dest.EmployeeId, act => act.Ignore())
            .ForMember(dest => dest.ContractPersonDataId, act => act.Ignore());
        cfg.CreateMap<HeadQuartersDTO, HeadQuarters>()
            .ForMember(dest => dest.Id, act => act.Ignore())
            .ForMember(dest => dest.IsActiv, act => act.Ignore())
            .ForMember(dest => dest.Adresa, act => act.MapFrom(src => src.Addresses)));
}