locked
An exception occurred while iterating over the results of a query for context type Invalid operation. The connection is closed. RRS feed

  • Question

  • User-72527073 posted

    I am working in a project using generic repository.

    This is my existing repository.

    namespace OnlineLearning.Repository
    {
      public interface IRepository<TModel> where TModel : class, IEntity
      {
        Task<PagedList<TModel>> GetPaginatedList(BaseParameter baseParameter);
        Task<IEnumerable<TModel>> GetAll();
        Task<TModel> GetById(Guid id);
        Task Insert(TModel entity);
        void Update(TModel entity);
        void Delete(Guid id);
      }
    }

    and later added method for bulk delete

    namespace OnlineLearning.Repository
    {
      public interface IRepository<TModel> where TModel : class, IEntity
      {
        Task<PagedList<TModel>> GetPaginatedList(BaseParameter baseParameter);
        Task<IEnumerable<TModel>> GetAll();
        Task<TModel> GetById(Guid id);
        Task Insert(TModel entity);
        void Update(TModel entity);
        void Delete(Guid id);
    
        void MultipleDelete(List<Guid> ids);
      }
    }

    Old methods are working as it is, but whenever i tried to update database using new methods i.e. MultipleDelete I am getting error.

    Microsoft.EntityFrameworkCore.Database.Command: Error: Failed executing DbCommand (56ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [s].[Id], [s].[Active], [s].[Address], [s].[ContactNumber], [s].[CreatedAt], [s].[CreatedByUserId], [s].[EmailAddress], [s].[IPAddress], [s].[LogoLocation], [s].[ModifiedAt], [s].[ModifiedByUserId], [s].[Name], [s].[SchoolCode] FROM [Schools] AS [s] WHERE [s].[Active] = N'Active'

    Microsoft.EntityFrameworkCore.Query[10100]
    An exception occurred while iterating over the results of a query for context type 'OnlineLearning.EntityFramework.Context.ApplicationDatabaseContext'.
    System.InvalidOperationException: Invalid operation. The connection is closed.
    at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
    at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
    at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown ---
    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
    --- End of stack trace from previous location where exception was thrown ---
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
    System.InvalidOperationException: Invalid operation. The connection is closed.
    at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
    at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
    at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown ---
    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
    --- End of stack trace from previous location where exception was thrown ---
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
    Microsoft.EntityFrameworkCore.Query: Error: An exception occurred while iterating over the results of a query for context type 'OnlineLearning.EntityFramework.Context.ApplicationDatabaseContext'.
    System.InvalidOperationException: Invalid operation. The connection is closed.
    at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
    at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
    at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown ---
    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
    --- End of stack trace from previous location where exception was thrown ---
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

    System.InvalidOperationException: Invalid operation. The connection is closed.
    at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
    at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
    at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

    Wednesday, June 10, 2020 2:20 AM

All replies

  • User475983607 posted

    Sounds like the DbContext is prematurely disposed. 

    You provided the interface but not the implementation. The community cannot review the generic repository design without the implementation.  

    Wednesday, June 10, 2020 9:47 AM
  • User-217098811 posted

    Hi pprajapati

    As mgeshard said,you can try to implement the interface member

    Multipledelete

    in your Repository

    like this

     public void MultipleDelete(List<Guid> ids)
            {
                throw new NotImplementedException();
            }

    If this still doesn't work, maybe this method is not universal.

    You can inherit your universal storage through custom storage and add this method to your custom storage.

    Finally, add your own custom warehousing to your Startup

    Hope this can help you

    Best regards

    yinqiu

    Friday, June 12, 2020 2:35 AM