Skip to content

Ef core migrations has runtime errors, can't use transaction when migrations! #2151

@niltor

Description

@niltor

Describe the issue or suggestion

In ef-core-migrations.md, there are some codes has runtime error!

After creating a MigrationsService and Worker.cs,just like:

    private static async Task RunMigrationAsync(CommandDbContext dbContext, CancellationToken cancellationToken)
    {
        var strategy = dbContext.Database.CreateExecutionStrategy();
        await strategy.ExecuteAsync(async () =>
        {
            // Run migration in a transaction to avoid partial migration if it fails.
            await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
            await dbContext.Database.MigrateAsync(cancellationToken);
            await transaction.CommitAsync(cancellationToken);
        });
    }

Afte running AspireHost, I got an error: User transaction is not supported with a TransactionSuppressed migrations or a retrying execution strategy.

So why use transaction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions