Pomelo.EntityFrameworkCore.MySql: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first.
Steps to reproduce
Here is the TagHelper code:
[HtmlAttributeName("identity-role")]
public string Role { get; set; }
public override async Task ProcessAsync(TagHelperContext context,
TagHelperOutput output)
{
List<string> names = new List<string>();
IdentityRole role = await roleManager.FindByIdAsync(Role).ConfigureAwait(true);
if (role != null)
{
foreach (var user in userManager.Users)
{
if (user != null
&& await userManager.IsInRoleAsync(user, role.Name).ConfigureAwait(true))
{
names.Add(user.UserName);
}
}
}
_ = output.Content.SetContent(names.Count == 0 ? "No Users" : string.Join(", ", names));
}
The line mentioned in the exception is: IdentityRole role = await roleManager.FindByIdAsync(Role).ConfigureAwait(true);
I can’t seem to get the Razor view code to paste properly here, but the line mentioned in the exception is here:
<td identity-role="@role.Id"></td>
The issue
Describe what is not working as expected.
If you are seeing an exception, include the full exceptions details (message and stack trace).
Exception message:
`An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first.
MySql.Data.MySqlClient.MySqlCommand.set_CommandText(string value) in MySqlCommand.cs, line 147`
Stack trace:
`InvalidOperationException: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first.
MySql.Data.MySqlClient.MySqlCommand.set_CommandText(string value) in MySqlCommand.cs
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable<T>+AsyncEnumerator.InitializeReaderAsync(DbContext _, bool result, CancellationToken cancellationToken)
Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.ExecuteAsync<TState, TResult>(TState state, Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable<T>+AsyncEnumerator.MoveNextAsync()
System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()
Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource> asyncEnumerable, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource> asyncEnumerable, CancellationToken cancellationToken)
MSLIS.Infrastructure.RoleUsersTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output) in RoleUsersTagHelper.cs
+
IdentityRole role = await roleManager.FindByIdAsync(Role).ConfigureAwait(true);
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.<RunAsync>g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count)
AspNetCore.Views_RoleAdmin_Index.ExecuteAsync() in Index.cshtml
+
<td identity-role="@role.Id"></td>
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Further technical details
MySQL version: MariaDb 10.4 Operating system: Windows 10 Pomelo.EntityFrameworkCore.MySql version: 3.1.1 Microsoft.AspNetCore.App version: 3.1
Other details about my project setup: Running this from Visual Studio 2019 Version 16.4.6 This runs perfectly when setup with MSSQL, it seems only when the mysql handler is used that the exception fires.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15
@bstewart-sdm This exception most commonly appears, when you use the same
DbContext
to execute a query, while iterating over the result of a previous one:The solution is to make sure, that you first fully read the data set returned by your query (e.g. by calling
.ToList()
):