【问题标题】:AutoMapper with dynamic type: Missing type map configuration or unsupported mapping具有动态类型的 AutoMapper:缺少类型映射配置或不支持的映射
【发布时间】:2016-08-13 05:56:03
【问题描述】:

答案:CreateMissingTypeMaps 必须设置为 true 才能使动态映射工作。

    public IEnumerable<T> GetDummies<T>(IEnumerable<dynamic> dummies)
    {
        var config =
            new MapperConfiguration(c => { c.CreateMissingTypeMaps = true; });

        IMapper mapper = config.CreateMapper();
        return dummies.Select(mapper.Map<T>).ToList();
    }

我有 Entity Framework 的包装器来执行对数据库的查询。我想让用户只选择必需的属性,但保留实体类型的结果。

这是虚拟代码(不使用 EF,但有同样的问题)

class Program
{
    static void Main(string[] args)
    {
        var dummies = new[]
        {
            new DummyContainer
            {
                Name = "First",
                Description = "First dummy",
                DummyNumbers = new List<int> { 1, 2, 3 },
                Foo = new FooThingy { Title = "Foo thingy" }
            }
        };

        var smallDummies = dummies.Select(d => new { d.Name }).ToList();
        List<DummyContainer> fullDummies = smallDummies.Select(Mapper.Map<DummyContainer>).ToList();

        Debugger.Break();
    }
}

class DummyContainer
{
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<int> DummyNumbers { get; set; }
    public FooThingy Foo { get; set; }
}

class FooThingy
{
    public string Title { get; set; }
}

得到这个异常:

Missing type map configuration or unsupported mapping.

Mapping types:
<>f__AnonymousType0`1 -> DummyContainer
<>f__AnonymousType0`1[[System.String, mscorlib, Version=4.0.0.0,  Culture=neutral, PublicKeyToken=b77a5c561934e089]] -> AutoMapperWithGenerics.DummyContainer

Destination path:
DummyContainer

Source value:
{ Name = First }

我有点卡在这里,因为文档指出 AutoMapper 使用属性名称映射回对象:Dynamic and ExpandoObject Mapping

请注意,上面的代码是示例。在我的应用程序中,事情变得有点疯狂,因为我实际上使用的是泛型,例如

Mapper.Map<TEntity>

... 它应该保持这种状态 - 我不知道使用什么实体类型。我的期望只是:将属性映射到现有类型,如果缺少,设置default(T)


编辑:我尝试将映射器从dynamic 指定到T,这里几乎是完整的代码:

class Program
{
    static void Main(string[] args)
    {
        // ...

        var dumminator = new DummyService();
        IEnumerable<DummyContainer> bigDummies = dumminator.GetDummies<DummyContainer>(smallDummies);

        Debugger.Break();
    }
}

class DummyService
{
    public IEnumerable<T> GetDummies<T>(IEnumerable<dynamic> dummies)
    {
        var config = new MapperConfiguration(c => c.CreateMap<dynamic, T>());
        IMapper mapper = config.CreateMapper();

        return dummies.Select(mapper.Map<T>).ToList();
    }
}

...这不会因异常而死,但是结果非常空(所有属性都有default 值。

【问题讨论】:

    标签: c# automapper automapper-4


    【解决方案1】:

    您可以全局配置它,而不是弃用 DynamicMap

    var config = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
    

    Reference 维基。

    【讨论】:

      【解决方案2】:

      您需要使用Mapper.DynamicMap&lt;T&gt; 而不是Mapper.Map&lt;T&gt; 来映射动态或匿名类

      List<DummyContainer> fullDummies = smallDummies.Select(Mapper.DynamicMap<DummyContainer>).ToList();
      

      【讨论】:

      • 感谢您这样指点我,您的建议已被弃用,但我能够找到如何通过配置对象进行设置。
      • 对,这是一种过时的方式。 Here 是关于配置它的最佳方式的文档(正如我所见,您已经找到了)。我很高兴能提供帮助。
      【解决方案3】:

      您好,类似的答案,但不要忘记在您的类上有一个私有构造函数,并且不验证内联映射。

             public class Player
             {
                 private Player() {}.....
      
             }
      
      
         var mapper = new MapperConfiguration(cfg =>
                  {
                      cfg.ValidateInlineMaps = false;
                      cfg.CreateMissingTypeMaps = true;
      
                  }).CreateMapper();
      
      
                  var player= mapper.Map<Player>(anonymousData);
      

      【讨论】:

        猜你喜欢
        • 2019-03-01
        • 2017-04-16
        • 2015-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-15
        • 2015-05-07
        • 1970-01-01
        相关资源
        最近更新 更多