A bit late, but could be helpful.
You can create a static helper class as:
public static class SqlFileTrigger {
public static string GetRawSql(string fileName) {
var baseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Migrations"); // your default migrations folder
var filePath = Path.Combine(baseDir, fileName);
return File.ReadAllText(filePath);
}
}
Then create your SQL files with similar naming convention followed by "{migrationName}_Up.sql" and "{migrationName}_Down.sql" like:
For example, assuming your created migration is named as "20230120230041_AddAuditingProperties.cs
"
This way they will be grouped under your migration .cs
file properly. (You can also create a separate folder for this but I usually prefer to keep them stacked so I can easily check if needed)
Then you can add "20230120230041_AddAuditingProperties_Up.sql
" as well as the "20230120230041_AddAuditingProperties_Down.sql
" (if down sql is needed)
Then for .NET EF:
public partial class AddAuditingProperties : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
Sql(Utils.SqlFileTrigger.GetRawSql("20230120230041_AddAuditingProperties_Up.sql"));
}
protected override void Down(MigrationBuilder migrationBuilder)
{
Sql(
Utils.SqlFileTrigger.GetRawSql("20230120230041_AddAuditingProperties_Down.sql"));
}
}
For .NET Core EF-Core
public partial class AddAuditingProperties : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(Utils.SqlFileTrigger.GetRawSql("20230120230041_AddAuditingProperties_Up.sql"));
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(
Utils.SqlFileTrigger.GetRawSql("20230120230041_AddAuditingProperties_Down.sql"));
}
}
PS: As a side note, if you are planing them to be deployed and run on server automatically, make sure you right click those created .sql files, and set "Build Action
" as "Content
" and "Copy to Output Directory
" as "Copy always
" - I had problems before when they were not set.