GZip Error with Code First - .NET Framework

Problem

When running a solution that uses Entity Framework Code First, the following error is encountered: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."

Cause

The cause is a value in the Model column of the __MigrationHistory table. This typically happens if you insert rows in their via a SQL script instead of using UPDATE-DATABASE within Visual Studio's Package Manager.

Solution

The SQL script should be constructed as follows. Note that the CONVERT function call MUST have the third parameter.

delete from dbo.[__MigrationHistory] where MigrationId = '201304042210224_MyMigration'

INSERT INTO dbo.[__MigrationHistory] (MigrationId, Model, ProductVersion)
select
     MigrationId    = '201304042210224_MyMigration' 
    ,Model          = CONVERT(varbinary(max),   
                        '0x1F8B0800000000000...'
                        ,1)
    ,ProductVersion = '5.0.0.net45'
where not exists (
    select 2 
    from dbo.[__MigrationHistory] 
    where MigrationId = '201304042210224_MyMigration'
    )