2017-06-26 29 views
0

一部の古いアプリケーションコードをデバッグするときに、例外が発生することがあります。Entity Framework ExecuteStoreCommands FirstOrDefaultメソッド中の例外

これは、コードの問題の行です:

XMLReferenceSequence lastXml = (from xml in _entities.XMLReferenceSequences 
             where xml.ServiceId == aService.Id 
             where xml.SourceTypeId == ETrainsSourceType.SOURCE_RAIL_XML 
             orderby xml.ReferenceDateTime descending 
             select xml).FirstOrDefault(); 

コードはほとんどの時間を動作し、これはめったに起こらないんことに留意すべきです。

この問題を解決する方法について考えていただければ幸いです。 FirstOrDefault()の前におそらくToList()を行うようにコードを再フォーマットすることを検討していましたか?例外の

スタックトレース:


エラー処理レールレコード。 :System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand、CommandBehaviorビヘイビア)でSystem.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute [TResultType](ObjectContext context、ObjectParameterCollection parameterValues System.DataでSystem.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func 1つの動作時System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func 1 FUNC、IDbExecutionStrategy executionStrategy、ブールstartLocalTransaction、ブールreleaseConnectionOnSuccess) で) ) 。 Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable 1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery 1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator 1.MoveNext() at System.Linq.Enumerable.FirstOrDefaul T [TSOURCE(IEnumerableを1 source) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable 1配列)System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle [TResult(IEnumerableを1 query, Expression queryRoot) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable 1ソース)で CHO.ETrains.Business.ETrainsManager.MovementProcessingFromRailListで (ETrainFeedDto aFeed )F:\ agent_work \ 4 \ s \ CHO.ALL \ Business \ CHO.ETrains.Business \ ETrainsManager.cs:行1961 CHO.ETrains.Business.etrainsManager.ProcessMessageOffServiceBus(ETrainFeedDto eTrainFeed、RequestDto req、HashSet`1 Fで& changesDetected、文字列schemaDir):\ agent_work \ 4 \ S \ CHO.ALL \ビジネス\ CHO.ETrains.Business \ ETrainsManager.cs:ライン379


受信したXMLメッセージことができませんでしたprocessed:System.Data.Entity.Core.EntityCommandExecutionException:コマンド定義の実行中にエラーが発生しました。詳細については、内部例外を参照してください。 ---> System.Data.SqlClient.SqlException:実行タイムアウトが期限切れです。操作が完了する前にタイムアウト時間が経過したか、サーバーが応答していません。 ---> System.ComponentModel.Win32Exception:待機操作がタイムアウトしました ---内部例外スタックトレースの終了--- System.Data.SqlClient.SqlConnection.OnError(SqlException例外、ブールbreakConnection、アクション1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action)1 wrapCloseInAction System.Data.SqlClient.TdsParser.TryRun(runBehavior runBehavior、SqlCommandオブジェクトcmdHandler、SqlDataReaderのデータストリーム、BulkCopySimpleResultSet bulkCopyHandler、TdsParserStateObject stateObj、ブールで)System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj、ブールcallerHasConnectionLock、ブールasyncClose)で & dataReady)at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.D System.Data.SqlClient.SqlCommandの に、System.Data.SqlClient.SqlCommandのata.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds、RunBehavior runBehavior、String resetOptionsString、Boolean isInternal、Boolean forDescribeParameterEncryption)が含まれます。RunExecuteReaderTds(のCommandBehavior cmdBehavior、RunBehavior runBehavior、ブールreturnStream、ブール非同期、のInt32タイムアウト、タスク&タスク、ブールasyncWrite、ブールinRetry、SqlDataReaderのDS、ブールdescribeParameterEncryptionRequest)System.Data.SqlClient.SqlCommand.RunExecuteReader(たCommandBehavior cmdBehavior、RunBehavior runBehaviorで 、System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func 3動作、TInterceptionContext interceptionContext、System.Data.Entity.Infrastructure.Interception.DbCommandDispatcherで処置3 executing, Action 3実行) ブールreturnStream、文字列方法、TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext 1 C) System.Data.Entity.Internal.InterceptableDbCommandで.Reader(たDbCommandコマンド、DbCommandInterceptionContext interceptionContext) System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand、CommandBehaviorの行動)で.ExecuteDbDataReader(CommandBehaviorの行動)System.Data.Common.DbCommand.ExecuteReader(CommandBehaviorの動作)で ---終了内部例外スタックトレース--- at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand、CommandBehaviorビヘイビア) at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute [ System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func 1 FUNC、IDbExecutionStrategy executionStrategy、ブールstartLocalTransaction、ブールreleaseConnectionOnSuccess)0でTResultType(ObjectContextは文脈、ObjectParameterCollection parameterValues) System.Data.Entity.Core.Objects.ObjectQuery 1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator 1でSystem.Data.Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable 1 forMergeOptionでSystem.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func 1つの動作時) ) 。 CHOでのMoveNext()System.Linq.Enumerable.FirstOrDefault [TSOURCE(IEnumerableを1 source) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable 1配列)で System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle [TResult(IEnumerableを1 query, Expression queryRoot) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable 1ソース)で Fで.ETrains.Business.ETrainsManager.MovementProcessingFromRailList(ETrainFeedDto aFeed):\ agent_work \ 4 \ S \ CHO.ALL \ビジネス\ CHO.ETrains.Business \ ETrainsManager.cs:ライン1961 CHO.ETrains.Business.ETrainsManagerで。 ProcessMessageOffServiceBus(ETrainFeedDe eTrainFee d、RequestDto req、HashSet`1 & changesDetected、String schemaDir)F:¥agent_work¥4¥s¥CHO.ALL¥Business¥CHO.ETrains.Business¥ETrainsManager.cs:CHO.WebJobs.xmlReceiveで393 行。 MessageProcessor.ProcessBrokeredMessage(BrokeredMessage msg)in F:¥agent_work¥4¥s¥CHO.WebJobs¥CHO.WebJobs.xmlReceive¥MessageProcessor.cs:line 102

+0

は2番目の 'where'が' && 'になるはずですが、明らかに問題ではありません。 'ToList()'コールは問題を解決するか、それともまだ続行しますか? –

+0

こんにちはRufus、私は別々のToListを試していなかった。誰かがこれを前に見ていたかどうかを知りたいだけなのです。私は&&の場所を変更するかもしれません。おそらく複数のwhereステートメントはクエリも複雑にしています。私はどこかでEFがマルチプルサブクエリに変換するかもしれないと読んでいると思います。 –

+0

VAR lastXmlRecord = _entities.XMLReferenceSequences .Where(X => x.TrainServiceId == aService.Id) .Where(X => x.SourceTypeId == ETrainsSourceType.SOURCE_RAIL_XML) .OrderByDescending(X => x.ReferenceDateTime) .ToList() .FirstOrDefault(); –

答えて

0

エラーメッセージは、メソッドが時間内に実行に失敗したことを示しています。

System.Data.SqlClient.SqlException:実行タイムアウトが切れています。動作前又は サーバの完了までの経過 タイムアウト期間は、SQL接続の接続は、デフォルトで30秒でCommandTimeoutプロパティを有する

に応答していません。この時間内にクエリが完了しない場合は、上記の例外がスローされます。この制限を超える可能性のある長い実行クエリがある場合は、接続文字列のこの値を大きくすることができます。理想的には、あなたはそれがあなたのクエリまたは問題の原因となっている別のクエリであるかどうかを判断することで、この問題を解決することができますhttps://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout(v=vs.110).aspx

:これを参照してください。クエリであれば、メンテナンス計画でデータを最適化したり、インデックスを作成したりすると、クエリが高速化されます。

もう1つの潜在的な危険性は、データを取得しているテーブルの1つをロックする長い実行クエリです。この場合、取得するデータにロックが設定されていないクエリを実行できます。この記事では、このエンティティフレームワークの実行方法を示します。 Entity Framework with NOLOCK

+1

何かの理由で、私の眼球はタイムアウトを見ていない。私はそれがもっと邪悪だと思って深く埋葬されました。 –

関連する問題