We use EF code-first + migrations in our project. We have also a lot of views, functions etc. which are in SQL currently in class stored as a string.
During migration (
dotnet ef migrations add "FOO"), we would like to add our SQL statements to the migration*.Designer.csfile to the modelBuilder instance in a waymodelBuilder.AddView("VIEW_NAME", "VIEW_SQL");
- During the next migration, we would like to use a
IMigrationsModelDifferto check differences between in models and generate operation for DbUp and DbDown in new migration.
The scenario could look like:
- New view
View1added to the project. dotnet ef migrations add "Migration1"generates a new migration:
- with
UpoperationmigrationBuilder.AddView("View1", "VIEW1 SQL"); - with
DownoperationmigrationBuilder.DropView("View1");
- New view
View2added to the project dotnet ef migrations add "Migration2"generates new migration only with newView2becauseView1was not changed from the latest migration.
- with
UpoperationmigrationBuilder.AddView("View2", "VIEW2 SQL"); - with
DownoperationmigrationBuilder.DropView("View2");
We know how to use this design stuff and how to override process of generating migration and Designer.cs files. We know how to override diff mechanism and generate new operations etc.
We don't know, what is the best way to keep our views, functions SQL statements in ModelBuilder instance. We can add as many annotations as SQL, this is an option, but we don't know if this is the only and the best option? Maybe there is another way how to do it?
Please advise.