【问题标题】:Why am I getting AutoMapping Exception为什么我得到自动映射异常
【发布时间】:2015-12-07 07:05:54
【问题描述】:

我得到了这个例外 -

缺少类型映射配置或不支持的映射。

映射类型: 列表`1 -> 我的类型

System.Collections.Generic.List`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] -> MyAssebmly.MyType

MyType 只是另一个具有必要属性的 POCO。 我的代码是 -

Mapper.CreateMap<AnotherType, MyType>()
            .ConvertUsing<CustomTypeResolver>();

在哪里

 internal class CustomTypeResolver : AutoMapper.ITypeConverter<AnotherType, 
 MyType>
 {
    //EDIT
    public MyType Convert(ResolutionContext context)
    {
        return new MyType { MyList = new List<T> { new T { ... } } };
    }
 }


 internal class MyType
 {
     public List<T> MyList { get; set; }
 }

任何人都知道出了什么问题。

【问题讨论】:

  • 我不明白泛型类型参数是如何工作的。 T 在 CustomTypeResolver 中是如何定义的?
  • 你能显示实际的映射调用吗?
  • 在完成所有 Automapper 设置(CreateMap 等...)之后,您是否致电 Mapper.AssertConfigurationIsValid

标签: c# automapper


【解决方案1】:

您已经定义了从AnotherTypeMyType 的映射,但考虑到该异常,您正试图将List 转换为MyType。我假设您想转换集合中的每个对象而不是整个集合(即List 对象),因此请相应地更新您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2018-06-22
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多