I am using EF.Core with code-first migrations to update a SQL database.
Whenever I added migrations (Package Manager Console: add-migration
), I am updating the database with the well-known update-database
command. Is there a way to run a SQL batch script after completion of this command automatically (like you can do with post-build events in Visual Studio) ?
That script could backup the database or do other tasks (like setting up user roles etc).
I don't want to modify existing migrations to do this, I know you can add something like
protected override void Up(MigrationBuilder migrationBuilder)
{
var sqlFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
@"Migrations\20200701103006_MySQLBatch_Up.sql");
var sqlCommands = System.IO.File.ReadAllText(sqlFile);
migrationBuilder.Sql(sqlCommands);
// ...
}
But I don't want to do that, because this way you would have to do it every time you're adding a new migration.
Is there an event or a method that one can override to achieve it? Or something that can be triggered, like a script?
Effectively, what I want to achieve is having a script or method invoking:
update-database
pg_dump -h localhost -U postgres -p 5432 myDatabase > C:\Temp\myDatabase.sql
Note: update-database runs in the Package Manager Context, pg_dump runs in the command shell (cmd.exe) - hence, you cannot run update-database in a .cmd or .bat script directly.