Timeout sql - Entity framework

0

Hej, mam problem z entity frameworkiem.
Mam poniższy kod:

private List<User> GetUsers()
        {
            var result = (from userAccount in this.Context.Set<UserAccount>()
                          select new User()
                          {
                              UserAccountID = userAccount.UserAccountID,
                              Email = userAccount.Email,
                              FirstName = userAccount.FirstName,
                              LastName = userAccount.LastName,
                          })
                    .ToList();

            return result;
        }

public void GetSimilarUsers(){
var users = GetUsers()
List<UserContact> usersContacts = GetUsersContacts(users.Select(x => x.UserAccountID).ToList());
}

Dlaczego stacktrace mi pokazuje jakby zapytanie do bazy szło w lini 18? Czy ten select wraz z ToList w parametrze wykonuje zapytanie do bazy? Czy projekcja nie odbyła się wcześniej?

Tu pełny stacktrace:

2022-01-24 16:06:08.0348 ERROR System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception: The wait operation timed out
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, 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.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   --- End of inner exception stack trace ---
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass41_0.<GetResults>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__31_0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Namespace.Sample.GetSimilarUsers(List`1 supplierIDs) line:18
0

Nie ta metoda. Pokaż GetUsersContacts

0
private List<UserContact> GetUsersContacts(List<long> userAccountIds)
        {
            var result = (from userContact in this.Context.Set<UserContactData>()
                          where userAccountIds.Contains(userContact.AccountId)
                          select new UserContact()
                          {
                              Number = userContact.Number,
                              Email = userContact.Email
                          })
                    .ToList();
            return result;
        }

Coś takiego, tylko jakby się tu wywaliło to nie4 było by stacktrace inne trochę?

1

Wygląda ok.
Może zobacz jakie sqle lecą do bazy.
A jak dodasz Include na adresy w pierwszej metodzie i pobierzesz od razu adresy z użytkownikami to też się występuje?

0

A wyjątek pokazuje wywalenie się w której części kodu? z lini 18 w pierwszym snippecie? Czy z tego zapytania w metodzie GetUsersContacts ?

0

IMO w tym przypadku z GetUsersContacts.

0

A co sądzisz o przypadku gdy lista userAccountIds w parametrze metody ma bardzo dużo elementów? Niestety nie jestem w stanie zreprodukować tego błędu u siebie, więc błądzę trochę.

0

Sądzę, że to słabe.
Ja bym pobrał userow z adresami w jednym zapytaniu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1