我使用了 automapper中的 AutoMapper.Data 组件
public static void Configure() { Mapper.Initialize(x => { x.AddDataReaderMapping(); x.AddProfile<DataSetProfile>(); }); }
public class DataSetProfile :Profile { public DataSetProfile() { CreateMap<DevicewareDto, IDataReader>(); CreateMap<IDataReader, DevicewareDto>(); } }
但是在使用的时候
var t = Mapper.Map<IDataReader, IEnumerable<DevicewareDto>>(ds.Tables[0].CreateDataReader());
出来的结果是 ds.Tables[0] 有多少行, IEnumerable<DevicewareDto> 就有多少个对象,但是每个对象的值都是空的, 没有填充进去
public class DevicewareDto { public string DeviceNumber { get; set; } public string SIMPhone { get; set; } public DateTime? ActivationTime { get; set; } public DateTime? LastUpdateTime { get; set; } public double Lng { get; set; } public double Lat { get; set; } public string Address { get; set; } public bool StatusLock { get; set; } public int? TENANT_ID { get; set; } public int? ORGANIZATION_ID { get; set; } public string OrganizationName { get; set; } public bool WorkStatus { get; set; } /// <summary> /// 连续工作时间(秒) /// </summary> public int WorkTime { get; set; } }
DeviceNumber SIMCCID SIMIMEI SIMPhone CreateTime IntervalType UploadFrequency UploadFrequencyModifyTime ActivationTime
645875654266 NULL NULL 13688888888 2017-08-25 23:53:36.000 NULL NULL NULL 2017-08-25 23:53:41.000
怎么能这样..
1 DataTable dt = new DataTable(); 2 dt.Columns.Add("DeviceNumber", Type.GetType("System.String")); 3 dt.Columns.Add("SIMCCID", Type.GetType("System.String")); 4 dt.Columns.Add("SIMIMEI", Type.GetType("System.String")); 5 dt.Columns.Add("SIMPhone", Type.GetType("System.String")); 6 dt.Columns.Add("CreateTime", Type.GetType("System.DateTime")); 7 dt.Columns.Add("IntervalType", Type.GetType("System.String")); 8 dt.Columns.Add("UploadFrequency", Type.GetType("System.String")); 9 dt.Columns.Add("UploadFrequencyModifyTime", Type.GetType("System.DateTime")); 10 dt.Columns.Add("ActivationTime", Type.GetType("System.DateTime")); 11 DataRow newRow; 12 newRow = dt.NewRow(); 13 newRow["DeviceNumber"] = "645875654266"; 14 newRow["SIMCCID"] = DBNull.Value; 15 newRow["SIMIMEI"] = DBNull.Value; 16 newRow["SIMPhone"] = "13688888888"; 17 newRow["CreateTime"] = Convert.ToDateTime("2017-08-25 23:53:36.000"); 18 newRow["IntervalType"] = DBNull.Value; 19 newRow["UploadFrequency"] = DBNull.Value; 20 newRow["UploadFrequencyModifyTime"] = DBNull.Value; 21 newRow["ActivationTime"] = Convert.ToDateTime("2017-08-25 23:53:41.000"); 22 dt.Rows.Add(newRow); 23 dt.TableName = "MoronTest"; 24 26 //AutoMapper.Mapper.CreateMap<IDataReader, DevicewareDto>().ForMember(des => des.DeviceNumber, opt => 27 //{ 28 //}); 29 30 AutoMapper.Mapper.CreateMap<IDataReader,List<DevicewareDto>>(); 31 List<DevicewareDto> list = AutoMapper.Mapper.Map<IDataReader, List<DevicewareDto>>(dt.CreateDataReader());
1 public class DevicewareDto 2 { 3 public string DeviceNumber { get; set; } 4 5 public string SIMCCID { get; set; } 6 7 public string SIMIMEI { get; set; } 8 9 public string SIMPhone { get; set; } 10 11 public DateTime? CreateTime { get; set; } 12 13 public string IntervalType { get; set; } 14 15 public string UploadFrequency { get; set; } 16 17 public DateTime? UploadFrequencyModifyTime { get; set; } 18 19 public DateTime? ActivationTime { get; set; } 20 }
你这种写法 在6.2 版本 行不通。映射数据是空的
请问你解决了吗, 我也遇到了这个问题, 一直无解
– 阿Q_Net 6年前@阿Q_Net: 额,我现在也遇到了 你解决了吗
– 暗暗大人 6年前