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

Need to modify some of the migration service example codes #2357

Open
calmbino opened this issue Jan 4, 2025 · 0 comments
Open

Need to modify some of the migration service example codes #2357

calmbino opened this issue Jan 4, 2025 · 0 comments
Labels
⌚ Not Triaged Not triaged

Comments

@calmbino
Copy link

calmbino commented Jan 4, 2025

An official documentation page that needs to be modified

You must modify some of the RunMigrationAsync method content in the Worker.cs example code in the SupportTicketApi.MigrationService project.

    private static async Task RunMigrationAsync(TicketContext 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);
        });
    }

Because ExecuteAsync manages transactions, an exception occurs when there is a custom (external) transaction.

Note

I have customised the User table by inheriting from IdentityUser.

System.NotSupportedException: User transaction is not supported with a TransactionSuppressed migrations or a retrying execution strategy.

A transaction was started before applying migrations. This prevents a database lock to be acquired and hence the database will not be protected from concurrent migration applications. The transactions and execution strategy are already managed by EF as needed. Remove the external transaction.

Therefore, it should be amended as follows

    private static async Task RunMigrationAsync(TicketContext dbContext, CancellationToken cancellationToken)
    {
        var strategy = dbContext.Database.CreateExecutionStrategy();
        await strategy.ExecuteAsync(async () =>
        {
            await dbContext.Database.MigrateAsync(cancellationToken);
        });
    }

or

    private static async Task RunMigrationAsync(TicketContext dbContext, CancellationToken cancellationToken)
    {
        var strategy = dbContext.Database.CreateExecutionStrategy();
        await strategy.ExecuteAsync(dbContext.Database.MigrateAsync, cancellationToken);
    }
@davidfowl davidfowl transferred this issue from dotnet/aspire Jan 4, 2025
@dotnetrepoman dotnetrepoman bot added the ⌚ Not Triaged Not triaged label Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⌚ Not Triaged Not triaged
Projects
None yet
Development

No branches or pull requests

1 participant