У меня проблемы с использованием. Net Core (3.1) с Mysql (поставщик Pomelo. mysql 3.1.1). У меня есть действительно базовая c функция (просто получить все записи из таблицы, однострочная функция), но по какой-то причине я всегда получаю исключение ObjectDisposedException со всей трассировкой стека, происходящей в Pomelo Provider. Кто-нибудь знает, что может быть причиной этого?
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'MySqlConnection'.
at MySql.Data.MySqlClient.MySqlConnection.VerifyNotDisposed() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 707
at MySql.Data.MySqlClient.MySqlConnection.get_Session() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 480
at MySqlConnector.Core.ICancellableCommandExtensions.ResetCommandTimeout(ICancellableCommand command) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ICancellableCommand.cs:line 42
at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
Код инициализации:
services
.AddDbContextPool<DeviceDbContext>(o => o
.UseMySql(connectionString, o => o
.ServerVersion(new ServerVersion(new Version(8, 0, 19), ServerType.MySql))
.MigrationsAssembly(typeof(DeviceDbContext).Assembly.GetName().Name)
.EnableRetryOnFailure()))
.AddHostedService<MigrationsHostedService<DeviceDbContext>>()
.AddHealthChecks()
.AddMySql(connectionString, "mysql");
return services;
, а затем его использование
services.AddDbContext();
позже, чем довольно просто:
private readonly DeviceDbContextdbContext;
public DeviceEntityFrameworkRepository(DeviceDbContext dbContext)
{
EnsureArg.IsNotNull(dbContext, nameof(dbContext));
this.dbContext = dbContext;
}
public async Task<IEnumerable<Device>> FindAllDevicesAsync()
=> await this.dbContext.Set<Device>().ToListAsync().ConfigureAwait(false);
и полный стек исключений
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'MySqlConnection'.
at MySql.Data.MySqlClient.MySqlConnection.VerifyNotDisposed() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 707
at MySql.Data.MySqlClient.MySqlConnection.get_Session() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 480
at MySqlConnector.Core.ICancellableCommandExtensions.ResetCommandTimeout(ICancellableCommand command) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ICancellableCommand.cs:line 42
at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
at System.Data.Common.DbCommand.ExecuteReaderAsync(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
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.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()