automapper 相关问题

用于.NET的基于约定的基于对象的对象映射器和转换器

Azure DevOps 管道在 .net 6 中恢复 AutoMapper NuGet 包时出现问题

我的管道运行时收到以下错误消息: 包 AutoMapper.Extensions.Microsoft.DependencyInjection 8.1.1 与 net60 (.NETFramework,Version=v6.0) 不兼容。套餐

回答 5 投票 0

如何让 Auto Mapper 使用继承列表类中所有内容的自定义列表

我将此设置为映射配置文件 创建地图(); 这个映射有效 StuffDto stuffDto = _mapper.Map(Stuff); 这个映射也有效 列表 我将此设置为映射配置文件 CreateMap<Stuff, StuffDto>(); 这个映射有效 StuffDto stuffDto = _mapper.Map<Stuff, StuffDto>(Stuff); 这个映射也有效 List<StuffDto> stuffDtoList = _mapper.Map<List<Stuff>, List<StuffDto>>(Stuff); 然而这个映射并不 PagesList<StuffDto> stuffDtoList = _mapper.Map<PagedList<Stuff>, PagesList<StuffDto>>(Stuff); 错误是:需要有一个带有 0 个参数或只有可选参数的构造函数。验证您的配置以获取详细信息。 PageList 看起来像 public class PagedList<T> : List<T> { public PagedList(IEnumerable<T> items, int count, int pageNumber, int pageSize) { CurrentPage = pageNumber; TotalPages = (int)Math.Ceiling(count / (double)pageSize); PageSize = pageSize; TotalCount = count; AddRange(items); } public int CurrentPage { get; set; } public int TotalPages { get; set; } public int PageSize { get; set; } public int TotalCount { get; set; } public static async Task<PagedList<T>> CreateAsync(IQueryable<T> source, int pageNumber, int pageSize) { // get the count of items EX 200 total events var count = await source.CountAsync(); var items = await source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync(); return new PagedList<T>(items, count, pageNumber, pageSize); } } 我需要做什么才能像 List 那样工作/解决? _mapper.Map<PagedList<Stuff>, PagesList<StuffDto>>(Stuff);

回答 0 投票 0

如何在 C# 中为子对象使用 AutoMapper?

我有两个班级: 公共类数据 { 公共字符串? Property1 { 得到;放; } 公共字符串? Property2 { 得到;放; } public int 数量 { get;放; } = 1; } 公开课 DataQuant...

回答 0 投票 0

如何使用 Automapper 将 ViewModel 中的 List<string> 映射到另一个实体中的字符串?

我有 4 个实体: 公开课游戏 { 公共 int Id { 得到;放; } 公共字符串游戏名称 { 得到;放; } public int DeveloperStudioId { 得到;放; } 公开

回答 1 投票 0

AutoMapper for JSON keys with same name as Model Fields

我有这个 AutoMapperProfile: 公共 AutoMapperJSON() { 创建地图() .ForMember(dest => dest.name, opt => opt.MapFrom(src => src.SelectTok...

回答 0 投票 0

AutoMapper - 找不到地图

我正在尝试在 C# 和 dotNET 中使用 AutoMapper,将实体转换为 DTO,反之亦然,但我不断收到相同的错误“未找到或不支持地图”。 所以我有实体,还有 DTO tha...

回答 1 投票 0

AutoMapper - 如何添加自定义 IObjectMapper

我想实现这里描述的东西 https://stackoverflow.com/a/34956681/6051621 https://dotnetfiddle.net/vWmRiY。 这个想法是打开一个 Wrapper。由于映射 Wrapper 我想实现这里描述的东西https://stackoverflow.com/a/34956681/6051621https://dotnetfiddle.net/vWmRiY. 想法是打开一个Wrapper<T>。由于开箱即用似乎不支持将 Wrapper<T> 映射到 T,因此可能的解决方案之一是注册自定义 IObjectMapper。 问题是 MapperRegistry 现在是内部的https://github.com/AutoMapper/AutoMapper/blob/master/src/AutoMapper/Mappers/MapperRegistry.cs#LL3C29-L3C31。 那么我该如何实现呢?我有更好的选择吗? 谢谢 最简单的方法是定义转换运算符: public class MyGenericWrapper<T> { public T Value {get; set;} public static explicit operator MyGenericWrapper<T>(T p) => new MyGenericWrapper<T> { Value = p }; public static explicit operator T(MyGenericWrapper<T> p) => p.Value; } 但如果你不想,那么你可以尝试以下操作: var config = new MapperConfiguration(cfg => { cfg.Internal().Mappers.Add(new SourceWrapperMapper()); cfg.Internal().Mappers.Add(new DestinationWrapperMapper()); cfg.CreateMap<Dto, Entity>(); cfg.CreateMap<Entity, Dto>(); }); var mapper = config.CreateMapper(); var entity = mapper.Map<Entity>(new Dto { SomeValue = new MyGenericWrapper<int>{Value = 42} }); var dto = mapper.Map<Dto>(entity); 让您入门的示例映射器: public class SourceWrapperMapper : IObjectMapper { public bool IsMatch(TypePair context) => IsWrappedValue(context.SourceType); public Expression MapExpression(IGlobalConfiguration configuration, ProfileMap profileMap, MemberMap memberMap, Expression sourceExpression, Expression destExpression) => Expression.PropertyOrField(sourceExpression, nameof(MyGenericWrapper<int>.Value)); private static bool IsWrappedValue(Type type) { return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(MyGenericWrapper<>); } } public class DestinationWrapperMapper : IObjectMapper { public bool IsMatch(TypePair context) => IsWrappedValue(context.DestinationType); public Expression MapExpression(IGlobalConfiguration configuration, ProfileMap profileMap, MemberMap memberMap, Expression sourceExpression, Expression destExpression) { var wrappedType = memberMap.DestinationType.GenericTypeArguments.First(); return Expression.Call(Mi.MakeGenericMethod(wrappedType), configuration.MapExpression(profileMap, new TypePair(memberMap.SourceType, wrappedType), sourceExpression, memberMap)); } public static MyGenericWrapper<T> Wrap<T>(T p) => new MyGenericWrapper<T> { Value = p }; private static MethodInfo Mi = typeof(DestinationWrapperMapper).GetMethod(nameof(Wrap), BindingFlags.Public | BindingFlags.Static); private static bool IsWrappedValue(Type type) { return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(MyGenericWrapper<>); } }

回答 1 投票 0

没有使用 Automapper 获取枚举类型的名称

想获取枚举的名称。我只得到 0 和 1。如何使用 Automapper 转换映射。任何人都为此建议解决方案。我的代码如下:- 公开课作业 { 公开

回答 0 投票 0

为什么负数在 AutoMapper 中被映射为零?

如果 src.memberBalance 为空,我修改了下面的代码以返回 0,但是,如果 src.memberBalance 为负,它仍然映射到 0 而不是负值。我确定这是...

回答 0 投票 0

Automapper 返回一个空集合,我想要一个 null

公开课人 { 名字{得到;放; } IEnumerable 地址 { get;放; } } 公共类 PersonModel { 名字{得到;放; } IEnumerable 地址 { ... public class Person { Name { get; set; } IEnumerable<Address> Addresses { get; set; } } public class PersonModel { Name { get; set; } IEnumerable<AddressModel> Addresses { get; set; } } 如果我像这样将Person映射到PersonModel: Mapper.DynamicMap<Person, PersonModel>(person); 如果 Addresses 上的 Person 属性为空,则它们在 PersonModel 上映射为空 Enumerable 而不是空值。 如何让 PersonModel 具有空的 Addresses 而不是空的 Enumerable? 简单的答案是使用AllowNullCollections: AutoMapper.Mapper.Initialize(cfg => { cfg.AllowNullCollections = true; }); 或者如果您使用实例 API new MapperConfiguration(cfg => { cfg.AllowNullCollections = true; } 除了在映射器配置初始化中设置AllowNullCollections(如this answer中所述),您还可以选择在AllowNullCollections定义中设置Profile,如下所示: public class MyMapper : Profile { public MyMapper() { // Null collections will be mapped to null collections instead of empty collections. AllowNullCollections = true; CreateMap<MySource, MyDestination>(); } } 所以可能有几种方法可以使用 Automapper 完成此操作,这只是其中一种: Mapper.CreateMap<Person, PersonMap>() .AfterMap( (src, dest) => dest.Addresses = dest.Addresses?.Any() ? dest.Addresses : null ); 此代码使用新的 c# ?. 运算符来实现空安全,因此如果您不能在代码中使用该功能,则可能需要删除它并检查是否为空。 另一种替代方法是使用条件,因此仅在值不为空时映射值。 这可能需要该值默认为空(因为它不会被映射) Mapper.CreateMap<Person, PersonModel>() .ForMember( dest => dest.Addresses, opt => opt => opt.Condition(source=> source.Addresses!= null)); 您应该能够为您想要此行为的属性定义自定义解析器。所以像: Mapper.CreateMap<Address, AddressModel>(); Mapper.CreateMap<Person, PersonModel>() .ForMember( dest => dest.Addresses, opt => opt.ResolveUsing(person => person.Addresses.Any() ? person.Addresses.Select(Mapper.Map<Address, AddressModel>) : null)); 如果您希望将此作为 API 中的一般规则,您可以像这样在配置服务方法中配置 Automapper。 public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddControllers(); [...] // configure automapping services.AddAutoMapper(cfg => cfg.AllowNullCollections = true, typeof(Startup)); } 或者,在单独的 DLL 中使用自动映射的情况下(例如:对于 DTO 服务),我更喜欢使用辅助函数,所以配置也应该在那里完成: public static class MappingHelper { private static readonly Lazy<IMapper> _lazy = new(() => { var config = new MapperConfiguration(cfg => { // This line ensures that internal properties are also mapped over. cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly; cfg.AddProfile<DomainToRepositoryProfile>(); cfg.AddProfile<RepositoryToDomainProfile>(); cfg.AllowNullCollections = true; }); var mapper = config.CreateMapper(); return mapper; }); public static IMapper Mapper => _lazy.Value; } 我无法使用 AutoMapper 12.0.1 和 Entity Framework Core 6.0.15 脚手架实体让 AllowNullCollections 与 net6 一起工作。唯一有效的方法是: internal class UserDtoMapper : Profile { public UserDtoMapper() { CreateMap<User, UserDto>() .ForMember(dest => dest.Votes, x => x.Condition(src => src.Votes != null && src.Votes.Any())) .ReverseMap(); } } public partial class User { // ... public virtual ICollection<Vote> Votes { get; set; } } public class UserDto { // ... public List<VoteDto>? Votes { get; set; } }

回答 7 投票 0

Automapper 中的条件映射列表条目

早上好。我需要根据正在“开票”的项目更新订单中项目的映射。目前,没有订单项的条件映射。我一直在看

回答 1 投票 0

Automapper 无法创建具有两个不同实体的地图

下面是两个实体 公共类用户 { public int UId { 得到;放; } 公共字符串 UName { 得到;放; } = 空! 公共字符串 UUsername { get;放; } = 空! ...

回答 0 投票 0

Automapper: System.InvalidOperationException: 'Coalesce used with type that cannot be null'

尝试在单元测试中使用映射器配置文件时出现此错误 错误: System.InvalidOperationException:'Coalesce 与不能为 null 的类型一起使用' 堆栈跟踪: 在 System.Linq.Express...

回答 2 投票 0

具有嵌套实体和不同表的 AutoMapper

我遇到上面的情况,我收到一个具有很多属性和两个嵌套 DTO 的 JSON 对象 (PreSalesDTO),我需要将它们映射到实际模型并将它们存储在不同的

回答 1 投票 0

C# 在删除重复项的同时将枚举数组自动映射到另一个枚举

我有两个枚举。一个比另一个具有更多的价值。我想在删除缺失值的同时将一个映射到另一个。 公共枚举 Person1ColoursEnum { 蓝色 = 1 红色 = 2 } 公共枚举

回答 1 投票 0

“LINQ 表达式‘DbSet’...”-EF Core + AutoMapper

当我尝试通过 AutoMapper 将数据转换为另一种类型时,我得到以下函数: System.InvalidOperationException:LINQ 表达式 'DbSet() 系统.

回答 0 投票 0

通过映射器配置文件中的aftermap对输入值放置条件

我通过 Aftermap 向配置文件映射器发送一个值。 var provinces = await _repository.AsQueryable().ToListAsync(); var result = provinces.Select(item => _mapper.Map(item, x...

回答 1 投票 0

如何在 Automapper 中映射数组对象的值?

我有以下结构,我想将 Parent.StockNumber 分配给 Supplier.StockNumber。由于 Supplier 是数组,我不确定如何在 Automapper 中使用 .ForEach。 代码: 公开课家长...

回答 1 投票 0

如何使用 AutoMapper 创建从开放泛型到简单泛型的自定义类型转换器?

我有一个从函数式程序语言实现选项 monad 的类。实现非常简单: 公开课 Maybe : IMaybe { 公共 T 值 { 得到;放; } =

回答 0 投票 0

ComponentNotRegisteredException 使用 AutoMapper 和 Autofac

尝试解析 AutoMapper.IMapper 实例时出现错误: Autofac.Core.Registration.ComponentNotRegisteredException:请求的服务“System.Object”尚未注册...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.