2017-10-07 144 views
1

試圖將DataTable轉換爲POCO列表。在StackOverflow找到了一些關於這個主題的答案,我認爲所有的東西都應該是O.K.但我沒有得到正確的結果。結果是五個項目的空列表。所以沒有從DataRow到POCO對象的映射。 有什麼想法可能是錯的?自動映射器將DataTable映射到POCO列表

AutoMapper是6.1.1版 AutoMapper.Data版本1.0.0

class Program 
{ 
    static void Main(string[] args) 
    { 
    var config = new MapperConfiguration(cfg => 
     { 
      cfg.CreateMissingTypeMaps = true; 
      cfg.CreateMap<IDataReader, Person>(); 
     }); 
     var mapper = config.CreateMapper(); 

     DataTable tbl = DataGenerator.MyPersons(); 

     var result = mapper.Map<List<Person>>(tbl.CreateDataReader()); 
    } 
} 

public static class DataGenerator 
{ 
    public static DataTable MyPersons() 
    { 
     DataTable myPersonssDataTable = new DataTable(); 
     myPersonssDataTable.Columns.Add(new DataColumn() 
     { 
      ColumnName = "FirstName", 
      DataType = typeof(string) 
     }); 
     myPersonssDataTable.Columns.Add(new DataColumn() 
     { 
      ColumnName = "LastName", 
      DataType = typeof(string) 
     }); 
     myPersonssDataTable.Columns.Add(new DataColumn() 
     { 
      ColumnName = "DateOfBirth", 
      DataType = typeof(DateTime) 
     }); 
     myPersonssDataTable.Columns.Add(new DataColumn() 
     { 
      ColumnName = "JobTitle", 
      DataType = typeof(string) 
     }); 
     myPersonssDataTable.Columns.Add(new DataColumn() 
     { 
      ColumnName = "TakenName", 
      DataType = typeof(string) 
     }); 
     myPersonssDataTable.Columns.Add(new DataColumn() 
     { 
      ColumnName = "IsAmerican", 
      DataType = typeof(bool) 
     }); 

     myPersonssDataTable.Rows.Add(new object[] { "Lenny", "Belardo", new DateTime(1971, 3, 24), "Pontiff", "Pius XIII", true }); 
     myPersonssDataTable.Rows.Add(new object[] { "Angelo", "Voiello", new DateTime(1952, 11, 18), "Cardinal Secretary of State", "", false }); 
     myPersonssDataTable.Rows.Add(new object[] { "Michael", "Spencer", new DateTime(1942, 5, 12), "Archbishop of New York", "", true }); 
     myPersonssDataTable.Rows.Add(new object[] { "Sofia", "(Unknown)", new DateTime(1974, 7, 2), "Director of Marketing", "", false }); 
     myPersonssDataTable.Rows.Add(new object[] { "Bernardo", "Gutierrez", new DateTime(1966, 9, 16), "Master of Ceremonies", "", false }); 


     return myPersonssDataTable; 
    } 
} 

public class Person 
{ 
    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    public DateTime DateOfBirth { get; set; } 

    public string JobTitle { get; set; } 

    public string TakenName { get; set; } 

    public bool IsAmerican { get; set; } 
} 

回答

1

你忘了添加DataReader的映射。

cfg.AddDataReaderMapping(); 

那麼你的代碼應類似於:

... 
var config = new MapperConfiguration(cfg => 
      { 
       cfg.AddDataReaderMapping(); 
       cfg.CreateMissingTypeMaps = true; 
       cfg.CreateMap<IDataReader, Person>(); 
      }); 
... 

我希望它能幫助。

+0

cfg.AddDataReaderMapping()在我的AutoMapper版本中是未知的。 我使用的是最後一個版本:AutoMapper是6.1.1版本的AutoMapper.Data版本1.0.0 –

+0

@DieterRuh'AddDataReaderMapping'是一個擴展函數,存儲在類'ConfigurationExtensions',程序集'AutoMapper.Data'中。檢查你是否使用AutoMapper.Data; – dbvega