Database-first strategy with EF Migrations?

c# database-migration entity-framework

Question

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?

1
41
2/13/2012 3:24:07 AM

Accepted Answer

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.

19
6/26/2016 4:34:17 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow