46

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?

2
  • 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. Commented Feb 13, 2012 at 5:18
  • 1
    If you want similar functionality to EF Migrations using database first, check out FluentMigrator Commented Oct 26, 2017 at 19:03

4 Answers 4

25

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.

Sign up to request clarification or add additional context in comments.

2 Comments

A good alternative might be Database projects / SSDT and doing schema compares.
See answer by tatigo
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.

1 Comment

It's the same case for SQlite database?
3

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.

Comments

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!

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.