2017-02-22 8 views
0

マイ例えば失敗:AutomapperはアイテムがConvertUsingを<>(使用コレクションを持つオブジェクト)

class BoxVM { 
    int BoxId {get;set;} 
    List<ItemVM> Items {get;set;} 
} 

class Box { 
    int BoxId {get;set;} 
    List<Item> Items {get;set;} 
} 

マッピングの設定で:

CreateMap<BoxVM, Box>(); 
CreateMap<ItemVM, Item>().ConvertUsing<ItemTypeConverter>(); 

BoxVM意志Itemsを変換する場合、ItemTypeConverterが呼び出されません。空のItemsコレクションをBoxに置いてください。

BoxIdが正しくマップされています。

ステップがありませんか?

答えて

0

動作しているようです。

using System.Collections.Generic; 

using AutoMapper; 

    [TestClass] 
    public class UnitTest1 
    { 
     [TestMethod] 
     public void TestMethod1() 
     { 

      Mapper.Initialize(cfg => 
       { 
        cfg.CreateMap<BoxVM, Box>(); 
        cfg.CreateMap<ItemVM, Item>().ConvertUsing<ItemTypeConverter>(); 

       }); 
      Mapper.AssertConfigurationIsValid(); 

      var boxVm = new BoxVM() 
      { 
       Value1 = "5", 
       Items = new List<ItemVM> { new ItemVM { Name = "Item1" } } 
      }; 

      var result = Mapper.Map<BoxVM, Box>(boxVm); 

      Assert.AreEqual(1, result.Items.Count); 
     } 
    } 

    public class Box 
    { 
     public string Value1 { get; set; } 
     public List<Item> Items { get; set; } 
    } 

    public class Item 
    { 
     public string Name { get; set; } 
    } 

    public class BoxVM 
    { 
     public string Value1 { get; set; } 
     public List<ItemVM> Items { get; set; } 
    } 

    public class ItemVM 
    { 
     public string Name { get; set; } 
    } 

    public class ItemTypeConverter : ITypeConverter<ItemVM, Item> 
    { 
     public Item Convert(ItemVM source, Item destination, ResolutionContext context) 
     { 
      return new Item { Name = source.Name }; 
     } 
    } 
+0

原因はプロパティ名に誤字がありました。 – PenFold

関連する問題