Entity framework 4.3 has introduced Automated Migration so that you don’t have to maintain database migration manually in code file for each change you make in your domain classes. You just need to run command in Package Manger Console and you will be done.
Let’s see how you can use automated migration.
As you know, you don’t have any database when you start writing code first application. For example, we start writing application with Student and Course entity classes. But before running application that is before creating database first time, you have to enable automated migration by running ‘enable-migrations’ command in Package Manager Console as below:
First, open package manager console from Tools → Library Package Manager → Package Manager Console and then run "enable-migrations –EnableAutomaticMigration:$true" command (make sure that default project is the project where your context class is)
Once command runs successfully, it creates internal sealed Configuration class in Migration folder in your project:
If you open and see this class then you will find AutomaticMigrationsEnabled = true in the constructor.
internal sealed class Configuration : DbMigrationsConfiguration<SchoolDataLayer.SchoolDBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(SchoolDataLayer.SchoolDBContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
You also need to set the database initializer in context class with new db initialization strategy MigrateDatabaseToLatestVersion as below:
public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base("SchoolDBConnectionString")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolDBContext, SchoolDataLayer.Migrations.Configuration>("SchoolDBConnectionString"));
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
As you can see in the above code that we have passed Configuration class name which was created by command, along with context class name.
Now you are set for automated migration. It will automatically take care of migration as and when you change the model. Run the application and see the created database:
You will find that it has created one system table __MigrationHistory along with other tables. This is where automated migration maintains the history of database changes.
Now let’s add Standard entity class and run the application again and see that it has automatically created Standard table.
Wait a minute, this will take care if you add new entity class or remove entity class but what about adding or removing properties from any of the entity class? To check that, let’s remove Description property from Standard class and run the application.
Oops.. you will get an error message:
This is because you will lose data in description column if you remove it from Standard class. So to handle this kind of scenario you have to set AutomaticMigrationDataLossAllowed = true in configuration class constructor along with AutomaticMigrationsEnabled = true.
Note: You can find more information about parameters we can pass to enable-migrations command using “get-help enable-migrations” command. For detailed help use “get-help enable-migrations –detailed”
So thus you can handle migration automatically.