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

Friday, June 19, 2015

Visual C# Technical Guru - May 2015

Another month as a judge in Microsoft TechNet Guru Awards under Visual C# category.

The TechNet Guru Awards celebrate the technical articles on Microsoft TechNet.

Post in WikiNinjas Official Blog,
image
Visual C# Technical Guru - May 2015
Happy Coding.

Regards,
Jaliya

Wednesday, June 17, 2015

Wrote a TNWiki Article Spotlight at Official Blog of TechNet Wiki

It's true that Universal Windows Platform (UWP) Apps are getting more and more popular with upcoming Windows 10. But I believe Windows Presentation Foundation (WPF) is still the pioneer technology for creating enterprise applications for Windows.

In TechNet Wiki Ninja’s blog, Tuesday is the day of the week that Wiki Ninjas write a post highlighting a TechNet Wiki article. So In this week’s TNWiki Article Spotlight, I wrote a post highlighting an article about WPF and it has won the April month's TechNet Guru Awards under Windows Presentation Foundation (WPF) Category, an article from Andy ONeill titled Change Tracking.

image
TNWiki Article Spotlight - WPF: Change Tracking
Read the post on,
   TNWiki Article Spotlight - WPF: Change Tracking

Happy Coding.

Regards,
Jaliya

Wednesday, June 3, 2015

Wrote a TNWiki Article Spotlight at Official Blog of TechNet Wiki

Got the pleasure of writing a post in Wiki Ninjas - Official Blog of TechNet Wiki. The title of the post was TNWiki Article Spotlight - Local Files.

In TechNet Wiki Ninja’s blog, Tuesday is the day of the week that Wiki Ninjas write a post highlighting a TechNet Wiki article. In this week I wrote a post about the article that won the April month's TechNet Guru Awards under Visual C# Category, an article from Andy ONeill titled Local Files.

image
TNWiki Article Spotlight - Local Files
Read the post on,
   TNWiki Article Spotlight - Local Files

Happy Coding.

Regards,
Jaliya