Can we run sql script using code first migrations?
I am new to code first and if I want to save my changes to a SQL script file before update-datab
What I like to do is to embed the SQL script as a resource in the assembly and use the SqlResource method. I have tested this approach with Visual Studio 2017 15.5.6.
First you need to create a migration file:
DbContext is definedIf you have both EF core and EF 6.x installed:
EntityFramework\Add-Migration RunSqlScript
If you have only EF 6.x Installed:
Add-Migration RunSqlScript
Add a Sql Script in the migration folder (I name it with the same prefix as the migration file)
In the File properties window make sure the Build Action is "Embedded Resource" Note that we don't need to copy to the output folder as the sql script will be embedded in the assembly.
Update the Up method in the RunSqlScript migration
public override void Up()
{
string sqlResName = typeof(RunSqlScript).Namespace + ".201801310940543_RunSqlScript.sql";
this.SqlResource(sqlResName );
}
I hope this helps