Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Database after Model Changes - Entity Framework 7

I have created an app using the lastest ASP.NET5 MVC 6 Entity Framework 7 and setup migrations using

dnx . ef migration add Initial
dnx . ef migration apply

This works but when I make a change to the model the database is not updated. I would like to have the database automatically update after a model change when I run the program.

My research only points me to old information that doesn't seem to be appropriate to Entity Framework 7.

My current code:

 public ApplicationDbContext(): base()
   {

        if (!_created)
        {

             Database.AsRelational().ApplyMigrations();
             _created = true;         
        }
  }

Can someone point me in the right direction?

I believe it use to work something like this:

Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
like image 725
Reafidy Avatar asked Jul 04 '15 04:07

Reafidy


1 Answers

You must manually run migrations with EF7 from command line, or call Database.Migrate from code, there is nothing automagic in EF7 (a deliberate decision) and after you change your model, create a new migration

like image 157
ErikEJ Avatar answered Nov 09 '22 11:11

ErikEJ