If I turn debugging on and connect my VS to the process, the error doesn't occur. Also I can run the code just fine from Visual Studio towards the same database. I do not Lazy Load. All Queries ens with ToFirstAsync() or ToListAsync() and has a Select to specify what to get.
This is how we make the call, from a Razorcomponent that displays My active cases:
List<CaseModel> Model { get; set; } = [];
protected override async Task OnInitializedAsync()
{
Model = (await _caseService.GetMyActiveList()).ToList();
_isLoading = false;
}
And here is the method GetMyActiveList() :
public async Task<IEnumerable<CaseModel>> GetMyActiveList()
{
await using var ctx = _dbContextFactory.CreateDbContext();
var userId = _userProvider.UserId;
var returnvalue = await ctx.Cases
.Where(item => item.CaseHandlers.Any(ch => ch.ReCordUserId == userId))
.Select(entity => new CaseModel
{
Id = entity.Id,
RegisterId = entity.RegisterId,
CaseNumber = entity.CaseNumber,
CaseStatusId = entity.CaseStatusId,
CaseTitle = entity.CaseTitle,
CreatedByUserId = entity.CreatedByUserId,
ClassificationCodeId = entity.ClassificationCodeId,
CaseTypeId = entity.CaseTypeId,
Confidentiality = entity.Confidentiality,
CaseCreatedDate = entity.CaseCreatedDate,
ClassificationCodeName = entity.ClassificationCode!.Name,
CaseTypeName = entity.CaseType!.Name,
CaseStatusName = entity.CaseStatus!.Name,
RegisterName = entity.Register!.Name,
CreatedByUserName = entity.CreatedByUser!.UserName,
})
.ToListAsync();
return returnvalue;
}
Here is the error in the log on Azure:
2025-02-15 08:40:37.986 +00:00 [Error] Microsoft.EntityFrameworkCore.Database.Command: Failed executing DbCommand (23ms) [Parameters=[@__ef_filter__TenantId_0='?' (DbType = Int32), @__userId_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT [c].[Id], [c].[RegisterId], [c].[CaseNumber], [c].[CaseStatusId], [c].[CaseTitle], [c].[CreatedByUserId], [c].[ClassificationCodeId], [c].[CaseTypeId], [c].[Confidentiality], [c].[CaseCreatedDate], [c2].[Name] AS [ClassificationCodeName], [c4].[Name] AS [CaseTypeName], [c6].[Name] AS [CaseStatusName], [r0].[Name] AS [RegisterName], [r1].[UserName] AS [CreatedByUserName]
FROM [Case] AS [c]
LEFT JOIN (
SELECT [c1].[Id], [c1].[Name]
FROM [ClassificationCode] AS [c1]
WHERE [c1].[TenantId] = @__ef_filter__TenantId_0
) AS [c2] ON [c].[ClassificationCodeId] = [c2].[Id]
INNER JOIN (
SELECT [c3].[Id], [c3].[Name]
FROM [CaseType] AS [c3]
WHERE [c3].[TenantId] = @__ef_filter__TenantId_0
) AS [c4] ON [c].[CaseTypeId] = [c4].[Id]
INNER JOIN (
SELECT [c5].[Id], [c5].[Name]
FROM [CaseStatus] AS [c5]
WHERE [c5].[TenantId] = @__ef_filter__TenantId_0
) AS [c6] ON [c].[CaseStatusId] = [c6].[Id]
INNER JOIN (
SELECT [r].[Id], [r].[Name]
FROM [Register] AS [r]
WHERE [r].[TenantId] = @__ef_filter__TenantId_0
) AS [r0] ON [c].[RegisterId] = [r0].[Id]
INNER JOIN [ReCordUser] AS [r1] ON [c].[CreatedByUserId] = [r1].[Id]
WHERE [c].[TenantId] = @__ef_filter__TenantId_0 AND EXISTS (
SELECT 1
FROM [CaseHandler] AS [c0]
WHERE [c0].[TenantId] = @__ef_filter__TenantId_0 AND [c].[Id] = [c0].[CaseId] AND [c0].[ReCordUserId] = @__userId_0)
2025-02-15 08:40:38.025 +00:00 [Error] Microsoft.EntityFrameworkCore.Query: An exception occurred while iterating over the results of a query for context type 'ReCord.Data.ApplicationDbContext'.
System.InvalidOperationException: Invalid operation. The connection is closed.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__211_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__292_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
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.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsyncTState,TResult
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
System.InvalidOperationException: Invalid operation. The connection is closed.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__211_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__292_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
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.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsyncTState,TResult
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
2025-02-15 08:40:38.026 +00:00 [Debug] Microsoft.EntityFrameworkCore.Infrastructure: 'ApplicationDbContext' disposed.
2025-02-15 08:40:38.044 +00:00 [Warning] Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer: Unhandled exception rendering component: Invalid operation. The connection is closed.
System.InvalidOperationException: Invalid operation. The connection is closed.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__211_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__292_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
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.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsyncTState,TResult
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsyncTSource
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsyncTSource
at ReCord.Business.Service.CaseService.GetMyActiveList() in D:\a\1\s\ReCord.Business\Service\CaseService.cs:line 62
at ReCord.Business.Service.CaseService.GetMyActiveList() in D:\a\1\s\ReCord.Business\Service\CaseService.cs:line 83
at ReCord.Api.Mirror.Controllers.CaseService.GetMyActiveList() in D:\a\1\s\ReCord.Api.Mirror\Controllers\CaseService.cs:line 9
at ReCord.Web.Components.Pages.Case.MyCasesComponent.OnInitializedAsync() in D:\a\1\s\ReCord.Web\Components\Pages\Case\MyCasesComponent.razor:line 31
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
2025-02-15 08:40:38.061 +00:00 [Error] Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Unhandled exception in circuit 'ShbvMWyOeK0mftzyGS30vVdKruSyVIRicebcAyFdaTg'.
System.InvalidOperationException: Invalid operation. The connection is closed.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__211_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__292_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
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.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsyncTState,TResult
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsyncTSource
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsyncTSource
at ReCord.Business.Service.CaseService.GetMyActiveList() in D:\a\1\s\ReCord.Business\Service\CaseService.cs:line 62
at ReCord.Business.Service.CaseService.GetMyActiveList() in D:\a\1\s\ReCord.Business\Service\CaseService.cs:line 83
at ReCord.Api.Mirror.Controllers.CaseService.GetMyActiveList() in D:\a\1\s\ReCord.Api.Mirror\Controllers\CaseService.cs:line 9
at ReCord.Web.Components.Pages.Case.MyCasesComponent.OnInitializedAsync() in D:\a\1\s\ReCord.Web\Components\Pages\Case\MyCasesComponent.razor:line 31
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)