读者在平行时封闭错误处理汽车应用程序
我使用 telerik openaccess ORM 从数据库获取数据。 我使用 ToList()
方法将它们存储在列表中。
然后,我使用 Parallel.ForEach
通过 AutoMapper 将模型对象映射到 DTO 对象。
在循环中第一次或第二次处理后,Parallel.ForEach 中出现错误:
DataStoreException: System.InvalidOperationException: Invalid attempt to call IsDBNull when reader is closed.
at System.Data.SqlClient.SqlDataReader.CheckHeaderIsReady(Int32 columnIndex, Boolean permitAsync, String methodName)
at System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
at OpenAccessRuntime.Data.IntConverter.Read(DataHolder& data)
at OpenAccessRuntime.Relational.RelationalGenericOID.CopyKeyFields(DataHolder& data)
at OpenAccessRuntime.Relational.fetch.FopGetOID.fetch(FetchResult fetchResult, StateContainer stateContainer)
at OpenAccessRuntime.Relational.fetch.FetchSpec.createRow(FetchResult fetchResult, StateContainer stateContainer)
我可以承认连接不是线程安全的,但为什么在使用 ToList() 后还需要此连接方法?使用此方法后应该检索所有数据。
是否存在另一种方法来做到这一点?
我的代码:
IEnumerable<Person> persons = database.GetData<Person>().ToList();
var safeList = new ConcurrentBag<PersonDTO>();
Parallel.ForEach(persons, (p) =>
{
var dto = Mapper.Map<PersonDTO>(p);
safeList.Add(dto);
});
return safeList.ToList();
I get data from a database by using telerik openaccess ORM.
I store these in a list by using ToList()
method.
Then I use a Parallel.ForEach
to map the model objects to DTO objects by using AutoMapper.
An error occurs in the Parallel.ForEach
after the first or second treatment in the loop:
DataStoreException: System.InvalidOperationException: Invalid attempt to call IsDBNull when reader is closed.
at System.Data.SqlClient.SqlDataReader.CheckHeaderIsReady(Int32 columnIndex, Boolean permitAsync, String methodName)
at System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
at OpenAccessRuntime.Data.IntConverter.Read(DataHolder& data)
at OpenAccessRuntime.Relational.RelationalGenericOID.CopyKeyFields(DataHolder& data)
at OpenAccessRuntime.Relational.fetch.FopGetOID.fetch(FetchResult fetchResult, StateContainer stateContainer)
at OpenAccessRuntime.Relational.fetch.FetchSpec.createRow(FetchResult fetchResult, StateContainer stateContainer)
I can admit the connection is not thread safe but why to need this connection yet after using the ToList()
method ? All data are supposed to be retrieved after using this method.
Does it exist another way to do it ?
My code:
IEnumerable<Person> persons = database.GetData<Person>().ToList();
var safeList = new ConcurrentBag<PersonDTO>();
Parallel.ForEach(persons, (p) =>
{
var dto = Mapper.Map<PersonDTO>(p);
safeList.Add(dto);
});
return safeList.ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论