GZip Error with Code First - .NET Framework


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."


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.


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)
     MigrationId    = '201304042210224_MyMigration' 
    ,Model          = CONVERT(varbinary(max),   
    ,ProductVersion = '5.0.0.net45'
where not exists (
    select 2 
    from dbo.[__MigrationHistory] 
    where MigrationId = '201304042210224_MyMigration'