Savepoint error on migrations
I wanted to pull out what I described here: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1841#issuecomment-2632533761 in case there is a chance for it to make it to the next build!
Is anyone having trouble with seeding data the new way (https://learn.microsoft.com/en-us/ef/core/modeling/data-seeding#configuration-options-useseeding-and-useasyncseeding-methods)?
I am getting:
fail: Microsoft.EntityFrameworkCore.Database.Transaction[20205]
An error occurred using a transaction.
An error occurred using a transaction.
MySqlConnector.MySqlException (0x80004005): SAVEPOINT __EFSavePoint does not exist
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 1081
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /_/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 131
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 487
at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 56
at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 309
at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108
at Microsoft.EntityFrameworkCore.Storage.RelationalTransaction.ReleaseSavepoint(String name)
Troubleshooting steps: https://github.com/dotnet/efcore/issues/33319#issuecomment-2030777176
Additional research: https://github.com/mysql-net/MySqlConnector/issues/1216
Hi @Donistivanov. I was getting the same error when running migrations with seeding too.
In my case, I believe it was occurring because dbContext.Database.AutoSavepointsEnabled is defaulting to true. Calling SaveChanges or SaveChangesAsync in the UseSeeding and UseAsyncSeeding callbacks was therefore implicitly committing a transaction that the migration tooling opens and expects to still be open on completion [1].
I was able to work around the issue by explicitly setting dbContext.Database.AutoSavepointsEnabled = false before calling SaveChanges and SaveChangesAsync in the UseSeeding and UseAsyncSeeding callbacks.
[1] https://github.com/dotnet/efcore/blob/3f7d40ec7be104358780955b3f0fea62495264dc/src/EFCore.Relational/Migrations/Internal/Migrator.cs#L197 and https://github.com/dotnet/efcore/blob/3f7d40ec7be104358780955b3f0fea62495264dc/src/EFCore.Relational/Migrations/Internal/Migrator.cs#L331
@smfeest Thank you. The workaround works for me.