如何在 AutoMapper 中配置 List 属性的映射?
How to configure map of List attribute in AutoMapper?
我是 AutoMapper 的新手。我有四个 类:
public class Source
{
public int Id {get;set;}
public List<SourceItem> items {get;set;}
}
public class SourceItem
{
public int Id {get;set;}
public string firstName {get;set;}
public string lastName {get;set;}
}
public class Destination
{
public int Id {get;set;}
public List<SourceItemDestination> items {get;set;}
}
public class SourceItemDestination
{
public int Id {get;set;}
public string firstName {get;set;}
}
是否可以创建映射配置文件以将源映射到目标允许在执行时引入 SourceItemDestination 列表mapper.Map?
完成!这是我的 classes:
using AutoMapper;
using AutoMapper.EquivalencyExpression;
using Infrastructure.Data.Context;
using Microsoft.Extensions.DependencyInjection;
using System;
using WebAPI.AutoMapper;
namespace WebAPI.Configurations
{
public static class AutoMapperConfig
{
public static void AddAutoMapperConfiguration(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddAutoMapper((serviceProvider, automapper) =>
{
automapper.AddCollectionMappers();
automapper.UseEntityFrameworkCoreModel<CellLabDbContext>(serviceProvider);
}, typeof(MappingProfile).Assembly);
}
}
}
using ApplicationCore.Domain;
using AutoMapper;
namespace WebAPI.AutoMapper
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<SourceItem, SourceItemDestination>().ReverseMap();
CreateMap<Source, Destination>().ReverseMap();
}
}
}
重要提示:我在映射列表时遇到了问题,因为来源 class 的 属性 items只读:
public class Source
{
public int Id {get;set;}
public List<SourceItem> items {get;}
}
通过这样做,映射器不会映射列表。但是,它不会引发任何错误或警告。
我是 AutoMapper 的新手。我有四个 类:
public class Source
{
public int Id {get;set;}
public List<SourceItem> items {get;set;}
}
public class SourceItem
{
public int Id {get;set;}
public string firstName {get;set;}
public string lastName {get;set;}
}
public class Destination
{
public int Id {get;set;}
public List<SourceItemDestination> items {get;set;}
}
public class SourceItemDestination
{
public int Id {get;set;}
public string firstName {get;set;}
}
是否可以创建映射配置文件以将源映射到目标允许在执行时引入 SourceItemDestination 列表mapper.Map?
完成!这是我的 classes:
using AutoMapper;
using AutoMapper.EquivalencyExpression;
using Infrastructure.Data.Context;
using Microsoft.Extensions.DependencyInjection;
using System;
using WebAPI.AutoMapper;
namespace WebAPI.Configurations
{
public static class AutoMapperConfig
{
public static void AddAutoMapperConfiguration(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddAutoMapper((serviceProvider, automapper) =>
{
automapper.AddCollectionMappers();
automapper.UseEntityFrameworkCoreModel<CellLabDbContext>(serviceProvider);
}, typeof(MappingProfile).Assembly);
}
}
}
using ApplicationCore.Domain;
using AutoMapper;
namespace WebAPI.AutoMapper
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<SourceItem, SourceItemDestination>().ReverseMap();
CreateMap<Source, Destination>().ReverseMap();
}
}
}
重要提示:我在映射列表时遇到了问题,因为来源 class 的 属性 items只读:
public class Source
{
public int Id {get;set;}
public List<SourceItem> items {get;}
}
通过这样做,映射器不会映射列表。但是,它不会引发任何错误或警告。