[英]Using AutoMapper with Data Reader
I went through How can I easily convert DataReader to List<T>? 我经历了如何轻松地将DataReader转换为List <T>?
I wanted to implement something like what is accepted as an answer in the above link. 我希望在上面的链接中实现类似于接受的内容。
Scenrio: Scenrio:
I am using OdbcDataReader to retrieve from the database. 我正在使用OdbcDataReader从数据库中检索。
And I have a Model Class . 我有一个模型类。 FYI , the properties of this class are exact replica of the column names from the database.
仅供参考,此类的属性是数据库中列名的精确副本。 I need to map these columns to the properties and return List Can this be accomplished using Automapper.
我需要将这些列映射到属性并返回List这可以使用Automapper完成。
Something like this 像这样的东西
public List<T> ReadData<T>(string queryString)
{
using (var connection = new SqlConnection(constr))
using (var command = new SqlCommand(queryString, connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
if (reader.HasRows)
return Mapper.DynamicMap<IDataReader, List<T>>(reader);
}
return null;
}
Define your class 定义你的课程
public class MarkType
{
public int id { get; set; }
public string name { get; set; }
public DateTime inserted { get; set; }
}
Use 采用
List<MarkType> lst = _helper.ReadData<MarkType>("SELECT [id],[name],[inserted] FROM [marktype]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.