且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有来自IDataReader的列表数据的AutoMapper

更新时间:2023-02-09 13:05:10

问题是Automapper也正在调用Read(),因此正试图始终查看第二个记录.如果您认为读取器中有1000行,那么AutoMapper如何将其转换为列表,而又不会遍历所有行都调用Read()?

The problem is that Automapper is calling Read() as well - so is trying to always look at the second record onwards. If you think about it if you have 1000 rows in the reader - how is AutoMapper going to convert that to a list without iterating through them all calling Read()?

更改您的行以呼叫HasRows

Change your line to call HasRows

例如

using (IDataReader dr = DatabaseContext.ExecuteReader(command))
    {
        if (dr.HasRows)
        {
            AutoMapper.Mapper.CreateMap<IDataReader, ProductModel>();
            return AutoMapper.Mapper.Map<IDataReader, IList<ProductModel>>(dr);
        }

        return null;
    }