我正在使用AutoMapper插件将 DataTable 映射到 C#对象。 这是我的代码:
public List<MyDto> GetReport()
{
List<MyDto> list = null;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<IDataReader, List<MyDto>>();
});
IMapper mapper = config.CreateMapper();
list = mapper.Map<IDataReader, List<MyDto>>(Odao.Inst.GetReport().Tables[0].CreateDataReader()).ToList();
return list;
}
我的 MyDto 类如下
public class MyDto
{
public int EmployeeId { get; set; }
public string FullName { get; set; }
}
在这里,数据正在正确地从我的DataAccess层返回,但是当它映射到Object时,列表将显示为空。我在这里想念什么吗?
答案 0 :(得分:0)
我通过使用3.3.1版的AutoMapper解决了这个问题。 基本上,版本4及更高版本将不支持此 IDataReader 功能。
现在,以下代码对我有用。
Mapper.Reset();
Mapper.CreateMap<IDataReader, MyDto>();
list = Mapper.Map<IDataReader,List<MyDto>>(Odao.Inst.GetReport().Tables[0].CreateDataReader()).ToList();