Wednesday, June 24, 2015

Entity Framework: Running Database Migrations without Visual Studio

When we are using Entity Framework, let’s say you want to run a database migrations on a machine which has no Visual Studio installed. In that kind of a scenario, you have two options.
  • Using SQL Script
  • Using migrate.exe
To show how these can be used, I am creating a console application and there I have the following.
public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
 
public class MyDbContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
}
Now I have enabled the migrations and added an initial migration and updated the database. Now I am adding a new property to Employee class.
public class Employee
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public DateTime Birthday { get; set; }
}
Now and I am adding a new migration named “AddedBirthdayToEmployee” and still I have not updated the database.


Using SQL Script

From the development machine which has Visual Studio installed, in the Package Manager Console, we can run the Update-Database command specifying  the –Script argument, so this time the changes will be written to a script rather than applied. Additionally we can specify the SourceMigration and the TargetMigration.
Update-Database -Script -SourceMigration: sourceMigrationName -TargetMigration: targetMigrationName
When you don’t specify a source migration, EF will use the last applied migration and when you don’t specify a target migration, EF will use the latest migration which has been created. For the demo, let’s just run Update-Database –Script command.

image
Result
And I will be prompted with a SQL script.

image
Migration Script
I can save this file, and run this script on the Database itself, so the database will get migrated.

image
Database Updated


Using migrate.exe

The next option is to use migrate.exe. You can find this tool inside {project}\packages\EntityFramework.{version}\tools. Copy migrate.exe to the folder where you have the assemblies. Now from the command prompt navigate to the folder where you have the assemblies along with migrate.exe and run the following command to see all the options of migrate.exe.
Migrate.exe /?
image
migrate.exe Options
You can see you a lot of options there. For the demo, I will just write execute the following command specifying the target connectionString and the connectionProviderName. Please note that /connectionString and /connectionProviderName must be specified together.
Migrate.exe EFMigrationsDemo.exe /connectionString="Data Source=.;Initial Catalog=EFMigrationsDemo.MyDbContext;Integrated Security=True" /connectionProviderName="System.Data.SqlClient"
image
Migrations Applied
You can see the migrations are getting applied and database is updated.

image
Database Updated

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment