我最近将我的 .net 核心升级到 3.0,并将 Automapper 从 6.2 升级到 9.0。现在,在 mapfrom 函数中使用 mapper.map 时,automapper 会抛出以下编译时错误。
CreateMap<DomainEntity, destination>()
.ForMember(dest => dest.userId, opt => opt.MapFrom(src => Mapper.Map<.UserInfo, string>(src.UserDetails)))
.ForMember(dest => dest.alertKey, opt => opt.MapFrom(src => src.Key));
Run Code Online (Sandbox Code Playgroud)
非静态字段、方法或属性“Mapper.Map(xxx)”需要对象引用
Automapper 在 Mapper 类方法的新升级中删除了 static 关键字。
我正在将 Visual Studio 2019 解决方案的项目从 AutoMapper 8.0.0 版升级到 9.0.0 版。代码中有很多地方调用了 ConfigureMap() 方法。构建输出状态中的错误:
IMappingOperationOptions<TSource, TDestination> 不包含 ConfigureMap 的定义,也没有可访问的扩展方法 ConfigureMap...
以下是当前代码的示例:
Mapper.Map(TSource, TDestination, opt => opt.ConfureMap());
Mapper.Map(TSource, TDestination, opt => opt.ConfigureMap().ForMember(dest => dest.someBool, m => m.MapFrom(src => src.someBoolVal));
我查看了 AutoMapper 的从 8.0.0 升级到 9.0.0 的文档,但没有提到 ConfigureMap() 方法被弃用。但是,当我搜索 VS 的对象浏览器时它没有出现。
如果有人可以分享如何在 9.0.0 中完成相同功能的代码,我将不胜感激。
我希望有人能帮助我。我们目前正在将 AutoMapper 从 v6 升级到 v9 - 我们会升级到 v10,但无法创建新的ResolutionContext影响我们的单元测试。也就是说,在 v9 中,我们在单元测试 AutoMapper 转换器方面仍然遇到以下问题......
转换器类:
public class FooBarConverter :
ITypeConverter<FooSourceObject, BarDestinationObject>
{
/// <inheritdoc/>
public virtual BarDestinationObjectConvert(FooSourceObjectsource, BarDestinationObjectdestination, ResolutionContext context)
{
EnsureArg.IsNotNull(source, nameof(source));
switch (source.Type)
{
case SomeType.None:
return context.Mapper.Map<NoneBarDestinationObject>(source);
case SomeType.FixedAmount:
return context.Mapper.Map<FixedBarDestinationObject>(source);
case SomeType.Percentage:
return context.Mapper.Map<PercentageBarDestinationObject>(source);
default:
throw new ArgumentOutOfRangeException(nameof(source));
}
}
Run Code Online (Sandbox Code Playgroud)
在 AutoMapper 6 之前,我们进行了以下单元测试(使用 Xunit):
public class FooBarConverterTests
{
private readonly FooBarConverter target;
private readonly Mock<IRuntimeMapper> mockMapper;
private readonly ResolutionContext resolutionContext;
public FooBarConverterTests()
{
this.mockMapper …Run Code Online (Sandbox Code Playgroud) 我使用的是版本 9。我使用的是基于配置文件的配置。当我运行应用程序 Mapper.Map<>() 方法抛出以下异常:
JobAssist.Services.ResumeBankMgmt.API.Application.ViewModels.ResumeBankViewModel 需要有一个带有 0 args 或只有可选 args 的构造函数。(参数“类型”)
我不知道是什么原因造成的。