为什么我得到“缺少类型映射配置或不支持映射”;自动器错误

本文关键字:映射 不支持 错误 配置 为什么 类型 | 更新日期: 2023-09-27 18:13:22

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
namespace TestAutomapper
{
  class Program
  {
    static void Main(string[] args)
    {
      var config = new MapperConfiguration(cfg => cfg.CreateMap<MyClassSource, MyClassDestination>());

      var mapper = config.CreateMapper();
      var source = new MyClassSource { DateTimeValue = null };
      var mapped = mapper.Map<MyClassSource, MyClassDestination>(source);
    }
  }
  public class MyClassSource
  {
    public object DateTimeValue { get; set; }
  }
  public class MyClassDestination
  {
    public DateTime? DateTimeValue { get; set; }
  }
}

错误是:

    AutoMapper.AutoMapperMappingException was unhandled
      HResult=-2146233088
      Message=Error mapping types.
    Mapping types:
    MyClassSource -> MyClassDestination
    TestAutomapper.MyClassSource -> TestAutomapper.MyClassDestination
    Type Map configuration:
    MyClassSource -> MyClassDestination
    TestAutomapper.MyClassSource -> TestAutomapper.MyClassDestination
    Property:
    DateTimeValue
      Source=Anonymously Hosted DynamicMethods Assembly
      StackTrace:
           at lambda_method(Closure , MyClassSource , MyClassDestination , ResolutionContext )
           at TestAutomapper.Program.Main(String[] args) in C:'Users'costa'documents'visual studio 2015'Projects'TestAutomapper'TestAutomapper'Program.cs:line 22
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
           HResult=-2146233088
           Message=Missing type map configuration or unsupported mapping.
    Mapping types:
    Object -> Nullable`1
    System.Object -> System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
           Source=Anonymously Hosted DynamicMethods Assembly
           StackTrace:
                at lambda_method(Closure , Object , Nullable`1 , ResolutionContext )
                at lambda_method(Closure , MyClassSource , MyClassDestination , ResolutionContext )
           InnerException: 

我以为这个错误已经解决了(https://github.com/AutoMapper/AutoMapper/issues/1095)。我使用的是Automapper 5.1.1.

如何解决这个问题?

感谢编辑:只是为了澄清,我关心的是空值的处理。我知道从非空对象值到DateTime的转换很复杂。在实际代码中,源对象中的实际值为null或DateTime。 编辑:

我创建了一个扩展方法ToDate来将对象转换为日期,并添加了这个映射来处理从对象到DateTime的转换。:

cfg.CreateMap<object, DateTime?>().ConstructUsing(src => src.ToDate());

为什么我得到“缺少类型映射配置或不支持映射”;自动器错误

由于源和目标类型中的属性具有相同的名称,AutoMapper将尝试从对象转换为日期时间?这是不可能的,这就是为什么你会得到你提到的错误。

您需要定义如何解析DateTime?财产。

var config = new MapperConfiguration(
    cfg =>
    {
        cfg.CreateMap<MyClassSource, MyClassDestination>()
            .ForMember(
                destination => destination.DateTimeValue,
                memberOptions => memberOptions.ResolveUsing(sourceMember =>
                {
                    DateTime dateTime;
                    return !DateTime.TryParse(sourceMember.DateTimeValue.ToString(), out dateTime) ? (DateTime?) null : dateTime;
                }));
    }
);

如果你的源成员是一个有效的日期时间对象,它将被转换为日期时间,否则目标属性将得到一个空值