Pomelo.entityframeworkcore.mysql: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first.

Created on 21 Mar 2020  路  2Comments  路  Source: PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

Steps to reproduce

            var layout = this.LayoutRepository.GetById(l => l.Id == id);
            layout.Company = this.CompanyRepository.GetById(
                c => c.Id == layout.CompanyId
            );
            layout.FileLayout = this.FileLayoutRepository.GetById(
                f => f.Id == layout.FileLayoutId
            );

The issue

I need returning 2 Object Children for a Parent Object. I'm using UnitOfWork Pattern.

Exception message:
Stack trace:
System.InvalidOperationException: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first.
   at MySql.Data.MySqlClient.MySqlCommand.set_CommandText(String value) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 150
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)

Further technical details

MySQL version: 10.3.22-MariaDB-0ubuntu0.19.10.1
Operating system: Ubuntu 19.10.1
Pomelo.EntityFrameworkCore.MySql version: 3.1.1
Microsoft.AspNetCore.App version: 3.0.0

Other details about my project setup:
ReactJS and .NET Core Web Api

closed-duplicate type-question

Most helpful comment

I am pretty sure this is a duplicate of the very recent #1047 and #1041

So it is very likely, that you are calling your code inside of a foreach loop, that directly iterates over a MyDbContext.MyDbSet property or IQueryable. You will need to execute the query first (resulting in an IEnumerable), before using it in the foreach loop.

See https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1047#issuecomment-601938455 for the explanation and solution.

All 2 comments

I am pretty sure this is a duplicate of the very recent #1047 and #1041

So it is very likely, that you are calling your code inside of a foreach loop, that directly iterates over a MyDbContext.MyDbSet property or IQueryable. You will need to execute the query first (resulting in an IEnumerable), before using it in the foreach loop.

See https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1047#issuecomment-601938455 for the explanation and solution.

@esdrasdemorais In case this is a duplicate, you can just close this issue, Otherwise, we will need more information about the code surrounding the exception and the current stack calls.

Was this page helpful?
0 / 5 - 0 ratings