Entity Framework– Code First with Automatic Migrations

Simply run the add-migration initial command in the package manager console window, browse to the Migrations folder, modify the new file that has been added, it will contain a class that inhereits DbMigration, with two methods, Up and Down. If you remove all of the content of the methods, but leave the methods, then run the Update-Database -Verbose command in the package manager console it will work.
To make things even easier, you can add the following code to your global.asax to make the automatic migrations happen automatically.
  1. public sealed class Configuration : DbMigrationsConfiguration  
  2.     {  
  3.         public Configuration()  
  4.         {  
  5.             this.AutomaticMigrationsEnabled = true;  
  6.             this.AutomaticMigrationDataLossAllowed = false;  
  7.         }  
  8.   
  9.         protected override void Seed(YourDbContext context)  
  10.         {  
  11.             //  This method will be called after migrating to the latest version.  
  12.   
  13.             //  You can use the DbSet.AddOrUpdate() helper extension method  
  14.             //  to avoid creating duplicate seed data. E.g.  
  15.             //  
  16.             //    context.People.AddOrUpdate(  
  17.             //      p => p.FullName,  
  18.             //      new Person { FullName = "Andrew Peters" },  
  19.             //      new Person { FullName = "Brice Lambson" },  
  20.             //      new Person { FullName = "Rowan Miller" }  
  21.             //    );  
  22.             //  
  23.         }  
  24.     }  
And then inside of your global.asax
  1. var migrator = new DbMigrator(new Migrations.Configuration());  
  2. migrator.Update();  

Search This Blog

Arsip Blog

Powered by Blogger.

Recent

Comment

Author Info

Like This Theme

Popular Posts

Video Of Day

jishnukanat@gmail.com

Sponsor

Most Popular