读者在平行时封闭错误处理汽车应用程序

发布于 2025-01-19 19:12:39 字数 1307 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文