自动映射器读取单个记录

时间:2018-09-13 18:35:09

标签: c# .net automapper

我一直在尝试使用AutoMapper读取单个Record。但是我无法使用下面的代码

    public ChartOfAccount GetSingleCOA( )
    {

        string queryString = "select * from ChartofAccounts where AccNo=423";
        using (var connection = new SqlConnection(conStr))
        using (var command = new SqlCommand(queryString, connection))
        {
            connection.Open();
            using (var reader = command.ExecuteReader())
                if (reader.HasRows)
                {
                    reader.Read();
                    return Mapper.Map<IDataRecord, ChartOfAccount>(reader);
                }
        }

        return null;
    }

1 个答案:

答案 0 :(得分:1)

只需将行更改为

 return Mapper.DynamicMap<IDataReader, ChartOfAccount>(reader);

解决了问题