Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First with DbUp

I am thinking of using Entity Framework 6 Code First for database interaction alongside DbUp for database schema update. The thing is I don't want to use EF migration for reasons. So, the workflow that I've reached to is:

  1. Change model (add POCOs, alter properties, etc)
  2. Run Add-Migration temp_file in Visual Studio Package Manager Console
  3. Run Update-Database -Script in Visual Studio Package Manager Console
  4. Grab the generated sql scripts including insertion of new row in table __MigrationHistory
  5. Create a new .sql file and past the generated scripts
  6. Delete temp_file
  7. Run DbUp

It works perfectly locally and on production server, however I don't feel comfortable with adding and then deleting temp_file every time a new migration is generated (I wish there were a way to permanently stop temp_file being added to the solution.).

So question: Is there a better way to do database migration with DbUp using Entity Framework?

like image 745
Hans Avatar asked Jan 28 '18 03:01

Hans


2 Answers

In most of the cases you can skip steps (2) and (6) by utilizing the Automatic Code First Migrations:

Automatic Migrations allows you to use Code First Migrations without having a code file in your project for each change you make.

By default automatic migrations are disabled. You enable them by adding the following like in your db migration configuration class constructor (usually called Configuration and located under Migrations sub folder):

AutomaticMigrationsEnabled = true;

Some things to consider:

  • Documentation states that automatic migrations have limitations, so be aware.
  • You can mix automatic and code based migrations (in other words, the proposed and your current approaches).
  • The benefits of your current approach is that you can preview how EF interprets your model changes and also add/remove/change parts of the migration code.
  • The automatic migrations have been deprecated (don't exist) in EF Core, so in EF Core projects you have to use something similar to your current approach, except that you have to keep the generated migration code files.
like image 97
Ivan Stoev Avatar answered Oct 13 '22 10:10

Ivan Stoev


Maybe this answer is too late, but maybe it will be useful as well. I completely understand your approach to use Entity Framework as ORM and a different tool for schema migration. But choosing DbUp requires you to write manually SQL or generate them as you described above. I suggest considering to use FluentMigrator instead of DbUp. It follows the same philosophy, but allows writing migration steps in C# using fluent syntax. In addition, it supports downgrades, i.e. rollback.

Here is an example:

[Migration(1)]
public class CreateUserTable : Migration
{
    public override void Up()
    {
        Create.Table("Users");
    }

    public override void Down()
    {
        Delete.Table("Users");
    }
}
like image 29
Boris Modylevsky Avatar answered Oct 13 '22 08:10

Boris Modylevsky