在调用已编译的查询后调用 ToList() 时,有时(!)会收到异常 - System.InvalidOperationException:阅读器关闭时调用 Read 的尝试无效。
为每个“调用”调用创建数据库上下文,查询在调用之前同步编译。这段代码被称为一次性 pro 文件,它是异步读取的。
查询以下列方式初始化、编译和调用。
public Func<SANAContext, int, string, IEnumerable<KeyValuePair<string, Importartikelhash>>> ImportingArticlesHashesQuery;
...
ImportingArticlesHashesQuery = EF.CompileQuery((SANAContext db, int LaLiNr, string lagerId) => db.Importartikelhash.AsNoTracking()
.Where(la => la.LieferantartikelId == LaLiNr && string.Compare(la.LagerId, lagerId, StringComparison.Ordinal) == 0).Select(a => new KeyValuePair<string, Importartikelhash>(a.LieferartikelNr, a)));
...
List<KeyValuePair<string, Importartikelhash>> records = ImportingArticlesHashesQuery.Invoke(context, supplier.SupplierInternalId, warehouse.Id).ToList();
在大量数据上我收到一个异常:
{System.InvalidOperationException: Invalid attempt to call Read when reader is closed.
at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
at System.Data.SqlClient.SqlDataReader.Read()
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Sanalogic.SupplierArticlesImport.SupplierDataParserApplication.Services.QueriesCompiler.DBHashValuesCache(PricatSupplier supplier, PricatWarehouse warehouse) in QueriesCompiler.cs:line 90}
什么会导致问题?我意识到异步操作存在一些问题,但我不知道在哪里搜索它。
非常感谢任何帮助!