gpt4 book ai didi

c# - Automapper 不与 .MapFrom 一起使用三元和计算值与 Net Core 2.2

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:24 25 4
gpt4 key购买 nike

我正在使用;

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/

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