如何在Automapper中使用数据集? [英] How can I use a dataset in Automapper?
本文介绍了如何在Automapper中使用数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用数据采集器作为源代码,但我想使用数据集。
I am currently using a datareader as the source but I want to instead use a dataset.
//datareader
AutoMapper.Mapper.CreateMap<IDataReader, AccountDTO>()
.ForMember(m => m.AccountId, opt => opt.MapFrom (r => r.GetInt32(r.GetOrdinal("AccountId"))))
.ForMember(m => m.ParentAccountId, opt => opt.MapFrom(r => r.GetInt32(r.GetOrdinal("ParentAccountId"))))
.ForMember(m => m.IsInactive, opt => opt.MapFrom(r => r.GetString(r.GetOrdinal("IsInactive"))))
.ForMember(m => m.AccountName, opt => opt.MapFrom(r => r.GetString(r.GetOrdinal("AccountName"))))
//dataset
AutoMapper.Mapper.CreateMap<DataSet, AccountDTO>()
.ForMember(m => m.AccountId, opt => opt.MapFrom(r => r.Tables[0].Columns[Constants.MappingFields.Accounts.AccountId]))
.ForMember(m => m.ParentAccountId, opt => opt.MapFrom(r => r.Tables[0].Columns[Constants.MappingFields.Accounts.ParentAccountId]))
.ForMember(m => m.IsInactive, opt => opt.MapFrom(r => r.Tables[0].Columns[Constants.MappingFields.Accounts.IsInactive]))
.ForMember(m => m.AccountName, opt => opt.MapFrom(r => r.Tables[0].Columns[Constants.MappingFields.Accounts.AccountName]))
.ForMember(m => m.AccountNumber, opt => opt.MapFrom(r => r.Tables[0].Columns[Constants.MappingFields.Accounts.AccountNumber]))
任何想法? >
any ideas?
推荐答案
我想使用数据集而不是datareader,所以我不保持打开数据库的连接。
I want to use the dataset instead of the datareader so I dont keep the connection to the database open.
我想我找到了解决方案;
I think I have found the solution;
- 创建数据集并关闭/处理连接
- 从数据表中创建一个datatablereader并传入
似乎正在工作。
DataTableReader dataTableReader = ds.Tables[0].CreateDataReader();
conn101.Close();
conn101.Dispose();
List<AccountDTO> accountDto1s = Mapper.Map<IDataReader, List<AccountDTO>>(dataTableReader);
这篇关于如何在Automapper中使用数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文