- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用;
AutoMapper.Extensions.Microsoft.DependencyInjection 6.0.0
在运行 net core 2.2
的 web api 项目中
在映射我的 DTO 对象时,我使用 Automapper 来映射几个字段;
public class AutoMapperProfile : AutoMapper.Profile
{
public AutoMapperProfile()
{
CreateMap<ReviewPostInputModel, Review>()
.ForMember(x => x.ReceiveThirdPartyUpdates, opt => opt.MapFrom(src => src.ReceiveThirdPartyUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.ReceiveUpdates, opt => opt.MapFrom(src => src.ReceiveUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.AverageScore, opt => opt.MapFrom(src => (decimal)Math.Round((src.Courtsey + src.Reliability + src.Tidiness + src.Workmanship) / 4, 2)));
// ...
}
}
在哪里;
using System;
using System.Collections.Generic;
using System.Text;
public class Review
{
// ...
public decimal Reliability { get; set; }
public decimal Tidiness { get; set; }
public decimal Courtsey { get; set; }
public decimal Workmanship { get; set; }
public decimal AverageScore { get; set; }
public DateTime? ReceiveUpdates { get; set; }
public DateTime? ReceiveThirdPartyUpdates { get; set; }
}
但是,当我尝试使用映射时;
var review = _mapper.Map<Review>(model);
除上面列出的我的 ForMember
外,所有标准成员都已映射,其中 DateTimes 设置为 DateTime
的新实例,Averagescore 设置为 0。
为了完整起见,我将映射器 DI 到我的 Controller 中,如下所示;
private readonly IMapper _mapper;
public ReviewController( IMapper mapper)
{
_mapper = mapper;
}
我在 StartUp.cs
中配置 Automapper,如下所示;
services.AddAutoMapper();
我还尝试向 Controller 添加测试以确认输入的值不是问题(在映射后完成并可以确认此值已正确更新);
review.AverageScore = (decimal)Math.Round((model.Courtsey + model.Reliability + model.Tidiness + model.Workmanship) / 4, 2);
有人知道为什么会发生这种情况吗?
最佳答案
您需要使用“ResolveUsing”而不是“MapFrom”
public class AutoMapperProfile : AutoMapper.Profile
{
public AutoMapperProfile()
{
CreateMap<ReviewPostInputModel, Review>()
.ForMember(x => x.ReceiveThirdPartyUpdates, opt => opt.ResolveUsing(src => src.ReceiveThirdPartyUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.ReceiveUpdates, opt => opt.ResolveUsing(src => src.ReceiveUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.AverageScore, opt => opt.ResolveUsing(src => (decimal)Math.Round((src.Courtsey + src.Reliability + src.Tidiness + src.Workmanship) / 4, 2)));
// ...
}
}
你可以看看这个答案: AutoMapper: What is the difference between MapFrom and ResolveUsing?
我们过去在从“ConfigureServices”添加自动映射器时遇到过问题,可能对您来说也是如此。你可以试着把这个:
AutoMapperConfiguration.Init();
在启动函数中并添加这个类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
namespace yournamespace.ViewModels.Mappings
{
public static class AutoMapperConfiguration
{
public static void Init()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ReviewPostInputModel, Review>()
.ForMember(x => x.ReceiveThirdPartyUpdates, opt => opt.MapFrom(src => src.ReceiveThirdPartyUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.ReceiveUpdates, opt => opt.MapFrom(src => src.ReceiveUpdates ? (DateTime?)DateTime.UtcNow : null))
.ForMember(x => x.AverageScore, opt => opt.MapFrom(src => (decimal)Math.Round((src.Courtsey + src.Reliability + src.Tidiness + src.Workmanship) / 4, 2)));
});
}
}
}
关于c# - Automapper 不与 .MapFrom 一起使用三元和计算值与 Net Core 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55633710/
AutoMapper 的 MapFrom 方法有两个重载: .MapFrom(Expression> 和 .MapFrom(string) 第一个重载可以这样使用: .ForMember(dest =
我有一个映射定义定义为 Mapper.CreateMap() .ForMember(dest => dest.TypeIndicator, src => src.ResolveUsing(new
已编辑:添加带有参数信息的构造函数。 我正在使用 AutoMapper 9.0 版和 .Net Core 2.1以下单元测试应更改 stockRecord.ClosePrice 的值,但如果存在第二个
有人告诉我,对于 1 个属性1 个属性映射,我应该使用 MapFrom,但是当以某种方式计算或修改目标属性时,我应该使用 ResolveUsing。当我使用这个映射时它仍然有效: .ForMember
忽略ResolveUsing采用 IValueResolver 的重载,并且仅查看这 2 个方法: void ResolveUsing(Func resolver); void MapFrom(Exp
使用版本 4 在做 map 时如何检查空值。我尝试了 .Value,但它不存在于 Null 中: Mapper.CreateMap() .ForMember(x
我一直在尝试将我的域对象映射到报 TableView 模型。在我伪造 Entity Framework 代码并使用构建器返回一个完全填充的 pocco 对象的测试中,一切都运行良好。现在我实际上正在访
我正在使用; AutoMapper.Extensions.Microsoft.DependencyInjection 6.0.0 在运行 net core 2.2 的 web api 项目中 在映射我
我已经成功使用 AutoMapper 一段时间了。但是,当我在 CreateMap 调用中使用 MapFrom() 时,出现上述错误。 我已升级到版本 2,似乎注册表已损坏。 CreateMap()
在将相同的具体类注册到不同的未命名实例时,我们看到 Unity 出现意外行为。在这种情况下,注册似乎以意想不到的方式相互干扰。 我们为我们想要注入(inject)的其他应用程序提供了一个框架不同的界面
我是一名优秀的程序员,十分优秀!