EF migrations / custom SQL for views, functions etc in model builder in designer.cs files

24 Views Asked by At

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.

  1. During migration (dotnet ef migrations add "FOO"), we would like to add our SQL statements to the migration *.Designer.cs file to the modelBuilder instance in a way

    modelBuilder.AddView("VIEW_NAME", "VIEW_SQL");

example place

  1. During the next migration, we would like to use a IMigrationsModelDiffer to check differences between in models and generate operation for DbUp and DbDown in new migration.

The scenario could look like:

  1. New view View1 added to the project.
  2. dotnet ef migrations add "Migration1" generates a new migration:
  • with Up operation migrationBuilder.AddView("View1", "VIEW1 SQL");
  • with Down operation migrationBuilder.DropView("View1");
  1. New view View2 added to the project
  2. dotnet ef migrations add "Migration2" generates new migration only with new View2 because View1 was not changed from the latest migration.
  • with Up operation migrationBuilder.AddView("View2", "VIEW2 SQL");
  • with Down operation migrationBuilder.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.

0

There are 0 best solutions below