41

We're using Database first approach with EntityFramework. We've several customers, and when we deploy new product version, we're now applying DB schema changes "manually" with tools like SQL Compare.

Is there a way how EF Migrations could help to apply changes to customers DB automatically?

Shaddix
  • 5,337
  • 6
  • 42
  • 77
  • of course, i'm not willing to loose anything, Code First Migration feature seems to preserve the data, I want something similar for DB first. I actually want this for simple scenarios - new tables added, new fields, etc. – Shaddix Feb 13 '12 at 05:18
  • 1
    If you want similar functionality to EF Migrations using database first, check out [FluentMigrator](https://github.com/fluentmigrator/fluentmigrator) – John Mo Oct 26 '17 at 19:03

4 Answers4

20

As far as I know, EF Migrations is a product targeted at CodeFirst and doesn't support Database First operations.

CodeFirst assumes that you will never make any changes manually to the database. All the changes to the database will go through the code first migrations.

Robert Harvey
  • 168,684
  • 43
  • 314
  • 475
Paul
  • 32,974
  • 9
  • 79
  • 112
5

I think there is! You need to continue your way through the code first.

To do this, Suppose that you have the following DbContext that EF Db first created for you:

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("Name=DefaultConnection")
    {

    }

    // DbSets ...
}

change that to the following to start using code first and all magic tools of it (migration, etc.):

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("YourDbFileName")
    {

    }

    // DbSets ...
}

It causes that EF creates a new connection string using SQL Express on your local machine in your web.config file with the name YourDbFileName, something just like the early DefaultConnection Db first created.

All you may need to continue your way, is that edit the YourDbFileName ConStr according to your server and other options.

More info here and here.

Amin Saqi
  • 17,037
  • 7
  • 43
  • 68
2

Starting Entity Framework 4.1 you can do Code First Migrations with an existing database.

So first you have the database, create the model, enable migrations.

The most important thing to remember that you should run Enable-Migrations before you do any changes to the schema since it should be in sync between your db and code.

tatigo
  • 1,874
  • 1
  • 25
  • 30
0

Just look for your DbContext child object and look for this method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

If you comment this:

throw new UnintentionalCodeFirstException();

then the exception would not be thrown on migration operation. As you might imagine, the migration look for this part to know what are the configurations for each entity with what table or tables.

Sorry if I didn't go with more details, if you wish more, I'll be happy to edit this and make it better!

  • This doesn't work with SQLite. You get the error: `System.NotSupportedException: Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.` – stigzler Nov 21 '18 at 19:40