1

When I run update-database -Script from the Nuget Package Manager Console, Visual studio generates a SQL script which I can run to update the database.

Is there any way to generate this SQL script in C# without running it?

That way I can show it to the user who is updating the application before updating the database.

1
  • Try update-database -Verbose Commented Feb 26, 2018 at 7:53

1 Answer 1

2

You can use DbMigrator and MigratorScriptingDecorator:

var configuration = new DbMigrationsConfiguration
{
    ContextType = typeof(ApplicationDbContext),
    MigrationsAssembly = typeof(ApplicationDbContext).Assembly,
    MigrationsNamespace = "YourNamespace.Migrations",
    AutomaticMigrationsEnabled = true
};
var dbMigrator = new DbMigrator(configuration);
var migratorScriptingDecorator = new MigratorScriptingDecorator(dbMigrator);
string script = migratorScriptingDecorator.ScriptUpdate(sourceMigration: null, targetMigration: null);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.