我有一个巨大的数据集试图执行,但是遇到以下错误;
Statement(s) could not be prepared.
堆栈跟踪
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
at Microsoft.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
at Microsoft.Data.SqlClient.SqlDataReader.Read()
at Microsoft.EntityFrameworkCore.Storage.RelationalDataReader.Read()
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at SalesForceReportSVC.Controllers.SalesForceDataLoaderController.<GetSDL>d__6.MoveNext() in C:\Users\JNyingi\source\repos\SalesForceReportSVC\SalesForceReportSVC\Controllers\SalesForceDataLoaderController.cs:line 60
以下语句引发异常
IQueryable<SalesForceProductionReportDto> dLProductions = this.repo_.salesforceLoader.FindAll();
IEnumerable<SalesForceProductionReportDto> reportDtos = dLProductions.ToList();
资料库
public IQueryable<SalesForceProductionReportDto> FindAll()
{
return this.salesForceReport.Set<SalesForceProductionReportDto>().AsNoTracking();
}
语境
private void ConfigureAPIIntegrations(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(this.APIdb, opt => opt.CommandTimeout((int)TimeSpan.FromMinutes(20).TotalSeconds));
optionsBuilder.EnableDetailedErrors();
}
我希望ToList
实现并从数据库加载数据。
编辑
甚至是像这样的简单Linq查询;
List<SalesForceProductionReportDto> salesForces = this.salesForceReport.productionReports.Select(x => x).ToList();
执行需要花费很多时间,但我可以在不到1秒的时间内执行SELECT
语句进行15000多个记录。
编辑2
我正在从如Context
所示的视图中进行选择
public virtual DbSet<SalesForceProductionReportDto> productionReports { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SalesForceProductionReportDto>().HasNoKey() .ToView("DailyProductionReport_Vw");
base.OnModelCreating(modelBuilder);
}
虽然我还没有收到关于它为什么不起作用的原因。
我使用FromSqlRaw
找到了解决上述问题的方法
因此,以下行正在运行;
List<SalesForceProductionReportDto> salesForces = this.salesForceReport.Set<SalesForceProductionReportDto>().FromSqlRaw<SalesForceProductionReportDto>("SELECT * FROM dbo.ProductionData_Vw;").ToList();