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

Adding transaction wrapper to migration #6322

Closed
arielcloud opened this issue Aug 15, 2016 · 5 comments
Closed

Adding transaction wrapper to migration #6322

arielcloud opened this issue Aug 15, 2016 · 5 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@arielcloud
Copy link

It look like no built-in configuration exists to execute update-database transactionally (so that the updates must succeed or fail as a complete unit)

Is not it is a basic and useful functionality? thanks!

@divega
Copy link
Contributor

divega commented Aug 16, 2016

@arielcloud individual migrations are (when they can) wrapped into their own transactions and that is by design. Anyway, @bricelam and I talked about this and we believe that if you invoke migrations programmatically and you wrap the call into a transaction also created on the context programmatically it is possible that will give you want you are asking for, e.g.:

using(var context = new MyContext())
{
  using(var transaction = context.Database.BeginTransaction())
  {
     context.Database.Migrate();
     transaction.Commit();
  }
}

The other (potentially simpler option) is to obtain the migration script and then execute it inside a transaction.

@divega divega closed this as completed Aug 16, 2016
@divega divega added the closed-no-further-action The issue is closed and no further action is planned. label Aug 16, 2016
@arielcloud
Copy link
Author

Before I opened this issue I searched it in google, and the only relevant result I saw was this one that cause me to think that no transaction exists by default, so maybe it'll be better to document this featuew somewhere

@dkutetsky
Copy link

EF Core generates transaction for each individual migration - to check, just use Package Manager Console:

Update-Database ... -verbose

But you could get generated sql and run it with wrapping it in transaction manually.

Just an working concept - there is no additional logic to not execute empty sql script, running migrations using from & to migrations, and other corner cases:

using (var dbContext = new DbContext())
{
   var migrator = dbContext.GetService<IMigrator>();	  
   
   // you could specify here fromMigration & toMigration instead of nulls
   var migrationSql = migrator.GenerateScript(null, null, true);
   
   //need this, because script contains "GO" statement for each migration that leads to execution error
   migrationSql = migrationSql.Replace("\r\nGO\r\n", "");
   
   //run all migrations in one transaction
   using (var transaction = dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted))
   {
       dbContext.Database.ExecuteSqlCommandAsync(migrationSql, cancellationToken);
       transaction.Commit();
   }
}

@Neme12
Copy link

Neme12 commented Dec 2, 2020

@divega Why is this closed? I couldn't find any way to make dotnet ef database update run everything in a single transaction.

@bricelam
Copy link
Contributor

bricelam commented Dec 3, 2020

@Neme12 I think you're looking for #22616

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

6 participants