Archive for the ‘.Net’ Category

Entity Framework Code First Migrations: Executing migrations using code, not PowerShell commands

Migrations are cool, but all the documentation only shows how to automatically update the database using the Visual Studio Package Mananager Console, like so:

Update-Database

However, I want my database to be updated through code like some of the older builds of Entity Framework Code First allowed.  Well, I did some digging, and here’s what I came up with.

var migratorConfig = new DbMigrationsConfiguration<NorthwindContext>();
migratorConfig.AutomaticMigrationsEnabled = true;

var dbMigrator = new DbMigrator(migratorConfig);
dbMigrator.Update();

If you’ve enabled migrations for your project using the Package Manager Console, you should have a file called Configuration.cs in your Migrations folder.  You can use this instead of using DbMigrationsConfiguration<>.  So with this setup, you’d have:

var migratorConfig = new ProjectNamespace.Migrations.Configuration();

var dbMigrator = new DbMigrator(migratorConfig);
dbMigrator.Update();

Lastly, you should be aware that the DbMigrator constror creates an instance of your DbContext.  So it’s very easy to create an infinite loop if you try to update the database from your DbContext contructor or from an implementation of IDbContextFactory.Create().  However, this is exactly what I wanted to do, so I just had to add some thread-safe checks around my database migration code:

public static int IsMigrating = 0;
private static void UpdateDatabase()
{
    if (0 == Interlocked.Exchange(ref IsMigrating, 1))
    {
        // Manually creating configuration:
        //var migratorConfig = new DbMigrationsConfiguration<NorthwindContext>();
        //migratorConfig.AutomaticMigrationsEnabled = true;

        // Using configuration defined in project:
        var migratorConfig = new ProjectNamespace.Migrations.Configuration();

        // 3
        //var dbMigrator = new DbMigrator(new Settings());
        var dbMigrator = new DbMigrator(migratorConfig);
        dbMigrator.Update();

        Interlocked.Exchange(ref IsMigrating, 0);
    }
}

Advertisements

Visual Studio Doesn’t Auto-Generate Code-Behind for XAML file

If you have a XAML file in a Visual Studio project, but for some reason, Visual Studio won’t auto-generate the code-behind for the XAML then make sure that the following line is in inside the <Project> tag of your project file:

<Import Project=”$(MSBuildBinPath)\Microsoft.WinFX.targets” />

The C# versus VB.Net debate in a Slightly Different Light (I Hope)

The C# versus Visual Basic.Net debate has been an ongoing one since the inception of the .Net languages.

Let me start off by saying that I am not so much concerned with the language/syntax itself as with how quickly I can finish my project using that language, and how good the code is afterward. I recently read a great quote: “Programmers don’t like to code, they like problem solving.” I could not agree more.
From a purely language/syntactical perspective, I can save you the trouble of reading this and tell you that it’s purely