Playing with EntityFramework Code First and “migrations” [5 minutes]

When EF 4 with CodeFirst came I was disappointed with the lack of automatic schema updates. Now a first version of “migrations” has arrived, which made me re-look at EF CodeFirst and make a small experiment – I was in a hurry and pretty sloppy, not reading anything more than briefly on this Scott Hanselman post. And I was pretty certain I was going to fail on at least one point in my experiment. But as it turned out it worked fine all the way.

This is what I did:

  • Create a new Console Application in Visual Studio
  • Add the EF Migrations Nuget (which also includes EF itself).
    PM> Install-Package EntityFramework.SqlMigrations
  • Add these classes:
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string SurName { get; set; }
    
    }
    public class DemoContext : DbContext
    {
        public DbSet<Person> People { get; set; }
    }
  • Make Main add some people:
    static void Main(string[] args)
    {
        var db = new DemoContext();
        db.People.Add(new Person { Name = "Test 1"});
        db.People.Add(new Person { Name = "Test 2"});
        db.SaveChanges();
    }
  • Run the program.

What happened here?

For me all went fine – no errors. Which meant the program either failed silently or I got a new database somewhere. I looked for an automatically created SQL CE DB file in my project folder, but none could be found. However I found a new DB in my SqlExpress instance, named ConsoleApplication1.DemoContext. Obviously it’s the convention for EF to create such and populate it with the necessary data. And yes, the table People was there with two records, Test 1 and Test 2. Very cool.

How about changing the schema a bit?

Now for some testing of the migrations part of this experiment.

  • Add the property Surname
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }
  • And some code in Main
    static void Main(string[] args)
    {
        var db = new DemoContext();
        db.People.Add(new Person { Name = "Test 3", Surname = "AAA" });
        db.People.Add(new Person { Name = "Test 4", Surname = "BBB" });
        db.SaveChanges();
    }
  • When I run this I got an expected error – because the newly added property does not have a corresponding field in the database table. Time to let the EF Migration perform it’s magic:
    PM> Update-Database
  • EF migrations takes some time to complete, when it’s done the schema is updated and the program runs fine. And the previously added names are still in the database.

I think EF SqlMigrations looks really nice. I’m sure it does not cover all and everything, but I can think of quite a few of my previous coding projects where I would have been in great benefit of this tool.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s