自动映射器将DataTable映射到POCO列表 [英] Automapper Map DataTable to POCO list

查看:59
本文介绍了自动映射器将DataTable映射到POCO列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将DataTable转换为POCO列表.在StackOverflow上找到了该主题的一些答案,我想一切都应该没问题.但我没有得到正确的结果.结果是五个项目的空列表.因此,没有从DataRow到POCO对象的映射.有什么想法可能是错误的吗?

Trying to convert a DataTable to a POCO list. Found some answers to that topic at StackOverflow and I thougth everything should be O.K. but I doesn't get the correct result. The result is an empty list of five items. So there is no mapping from the DataRow to the POCO object. Any thoughts what can be wrong?

AutoMapper是6.1.1版

AutoMapper is version 6.1.1

AutoMapper.Data版本1.0.0

AutoMapper.Data version 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; }
}

推荐答案

您忘记添加DataReader映射.

You forgot to add the DataReader mapping.

cfg.AddDataReaderMapping();

然后您的代码应类似于:

Then your code should look similar to:

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

希望对您有帮助.

这篇关于自动映射器将DataTable映射到POCO列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆