Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperationException thrown when RelationalDataReader reads RecordsAffected #1122

Closed
Executor-Cheng opened this issue Jan 25, 2022 · 3 comments

Comments

@Executor-Cheng
Copy link

Executor-Cheng commented Jan 25, 2022

Software versions
MySqlConnector version: 2.1.3
Server type (MySQL, MariaDB, Aurora, etc.) and version: MariaDB 10.5.5
.NET version: 6.0.0
Microsoft.EntityFrameworkCore version: 6.0.0
Pomelo.EntityFrameworkCore.MySql version: 6.0.0

Describe the bug
When the query fetches rows from the database, it will throw InvalidOperationException.

Exception

fail: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[48]
      An error occurred while reading the key ring.
      System.InvalidOperationException: Can't call this method when MySqlDataReader is closed.
         at MySqlConnector.MySqlDataReader.VerifyNotDisposed() in /_/src/MySqlConnector/MySqlDataReader.cs:line 653
         at MySqlConnector.MySqlDataReader.GetResultSet() in /_/src/MySqlConnector/MySqlDataReader.cs:line 658
         at MySqlConnector.MySqlDataReader.get_RecordsAffected() in /_/src/MySqlConnector/MySqlDataReader.cs:line 205
         at Microsoft.EntityFrameworkCore.Storage.RelationalDataReader.Dispose()
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.Dispose()
         at Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.EntityFrameworkCoreXmlRepository`1.<GetAllElements>g__GetAllElementsCore|3_0()+MoveNext()
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
         at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
         at Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.EntityFrameworkCoreXmlRepository`1.GetAllElements()
         at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.GetAllKeys()
         at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.CreateCacheableKeyRingCore(DateTimeOffset now, IKey keyJustAdded)
         at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.ICacheableKeyRingProvider.GetCacheableKeyRing(DateTimeOffset now)
         at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRingCore(DateTime utcNow, Boolean forceRefresh)
System.InvalidOperationException: Can't call this method when MySqlDataReader is closed.
         at MySqlConnector.MySqlDataReader.VerifyNotDisposed() in /_/src/MySqlConnector/MySqlDataReader.cs:line 653
         at MySqlConnector.MySqlDataReader.GetResultSet() in /_/src/MySqlConnector/MySqlDataReader.cs:line 658
         at MySqlConnector.MySqlDataReader.get_RecordsAffected() in /_/src/MySqlConnector/MySqlDataReader.cs:line 205
         at Microsoft.EntityFrameworkCore.Storage.RelationalDataReader.DisposeAsync()
         at Microsoft.EntityFrameworkCore.Storage.RelationalDataReader.DisposeAsync()
         at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToArrayAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)

Code sample
await DbContext.Set<T>.FirstOrDefaultAsync();
await DbContext.Set<T>.ToArrayAsync();

@Executor-Cheng Executor-Cheng changed the title Unable to execute query which only fetches one result from the database Unable to execute query which fetches result from the database Jan 25, 2022
@bgrainger
Copy link
Member

It appears that RelationalDataReader closes the data reader, then reads .RecordsAffected from it: https://github.com/dotnet/efcore/blob/511b6b8b6d9913c3e5fbf243444cc9f775ae6b99/src/EFCore.Relational/Storage/RelationalDataReader.cs#L105-L114

That would be broken by this recent change in 2.1.3: 7f74a400

@Executor-Cheng for now I think you will need to downgrade to 2.1.2.

CC @lauxjpn It may be advisable to skip MySqlConnector 2.1.3 as a dependency for Pomelo and wait until 2.1.4 is available to fix this issue.

@bgrainger
Copy link
Member

Fixed in 2.1.4. CC @lauxjpn

@bgrainger bgrainger changed the title Unable to execute query which fetches result from the database InvalidOperationException thrown when RelationalDataReader reads RecordsAffected Jan 25, 2022
@lauxjpn
Copy link
Contributor

lauxjpn commented Jan 26, 2022

We will release the next version of Pomelo, that will then reference the latest version of MySqlConnector, after the EF Core team has released 6.0.2, which I expect to be in the first half of February.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants