Exception: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first
Description
We are using IncludeFilter to get elements filtering by some property.
In our example we have one entity called Tariff wich has childs called SubElement. When we try to get a set of elements of Tariff with some of the SubElements using IncludeFilter it throws an exception.
The code that throws the exception:
using (var ctx = new MyDbContext(optionsBuilder.Options))
{
PrepareDatabase(ctx);
try
{
var set2 = ctx.Tariffs.IncludeFilter(f => f.SubElements.Where(s=> s.IsSoftDeleted == true)).ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
Exception
Exception message: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first.
Stack trace: 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.Internal.RelationalCommand.CreateCommand(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlConverterCommandBuilderFactory.MySqlConverterRelationalCommandBuilder.MySqlConverterRelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
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 Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)+MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at Z.EntityFramework.Plus.QueryFutureEnumerable`1.SetResult(IEnumerator`1 enumerator)
at Z.EntityFramework.Plus.QueryFutureEnumerable`1.SetResult(DbDataReader reader)
at Z.EntityFramework.Plus.QueryFutureBatch.ExecuteQueries()
at Z.EntityFramework.Plus.QueryFutureEnumerable`1.GetEnumerator()
at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Z.EntityFramework.Plus.QueryIncludeFilterParentQueryable`1.CreateEnumerable()
at Z.EntityFramework.Plus.QueryIncludeFilterParentQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at EfPlusPomelo.Program.Main(String[] args) in C:\proyectos\EfPlusPomelo\Program.cs:line 21
Fiddle or Project
Further technical details
- EF version: 2.2.6
- EF Plus version: 2.0.31
- Database Provider: Pomelo.EntityFrameworkCore.MySql 2.2.6
Hello @DavidOchando ,
Thank you for reporting, we will look at it.
Best Regards,
Jon
Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework Extensions • Entity Framework Classic • Bulk Operations • Dapper Plus
Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval Function • SQL Eval Function
Hi Jonathan.
I tested it with the last stable version (3.0.31) and it doesn't throw any exception, but it seems that the filters is not applied.
EF Plus version: 3.0.32. Exactly same error using the simplest IncludeFilter. The library seems to be unusable at the moment.
Hello All,
The v3.0.33 has been released.
Unfortunately, we didn't find the right way to fix it. The library will now execute multiple MySQL statements when the provider Pomelo.EntityFrameworkCore.MySql is used (the issue doesn't happen with the official MySQL provider).
We will still work on this issue but meanwhile, we thought it was the best quick fix as we already passed way too many time in the past 2 months trying to find a solution and no any progress happened ;(
Any update on this in version 3.1.1 for Pomelo.EntityFrameworkCore.MySql v 3.1.1.0. I am facing exactly same issue. Please update.
This link 17788 says The official one from MySQL: MySql.Data.EntityFrameworkCore. As of now, the latest version is 8.0.19, and works with Entity Framework Core 2.1 (and probably also 2.2). Since EF Core 3.0 is a major version with breaking changes, you cannot use it with this provider. The Pomelo provider: Pomelo.EntityFrameworkCore.MySql. There is a 3.1 version of this provider.
so what should we do, none of them is working either in 1 or the other way.
From what I remember, the latest version of our library work.
However, multiple queries are sent instead of only one, as batching all queries in the same command doesn't work with Pomelo.