title: Asp.net Mvc MigrationSqlGenerator for Postgresql gives me an error
body: <p>I've disabled automatic migration and I created a new MigrationSqlGenerator for Postgresql:</p>
  
  <pre><code>    public Configuration()
      {
          AutomaticMigrationsEnabled = false;
          SetSqlGenerator("Npgsql", new PostgreSqlMigrationSqlGenerator());
      }
  </code></pre>
  
  <p>From the Package Manager Console, I execute the command "Update-Database -Verbose". All the migrations are executed correctly and It create the Schema and all the Tables (including the __MigrationHistory table) perfectly. At the end of creation, It show me this error:</p>
  
  <pre><code>    System.FormatException: Input string was not in a correct format.
     at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&amp; number, NumberFormatInfo info, Boolean parseDecimal)
     at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
     at System.Convert.ToInt32(String value)
     at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildColumnModel(XElement property, String entitySetName, ModelMetadata modelMetadata)
     at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildAlterColumnOperation(String table, XElement targetProperty, String targetEntitySetName, ModelMetadata targetModelMetadata, XElement sourceProperty, String sourceEntitySetName, ModelMetadata sourceModelMetadata)
     at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.&lt;FindChangedColumns&gt;b__ef(&lt;&gt;f__AnonymousType1d`2 &lt;&gt;h__TransparentIdentifiere5)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString)
     at System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration)
     at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
     at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
     at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
  Input string was not in a correct format.
  </code></pre>
  
  <p>The __MigrationHistory is empty. No one migration is register.
  I've clone the EF project and It seems to get the error in the following method:</p>
  
  <pre><code>public IEnumerable&lt;MigrationOperation&gt; Diff(XDocument sourceModel, XDocument targetModel, bool? includeSystemOperations = null)
      {
          DbProviderInfo providerInfo;
  
          _source
              = new ModelMetadata
                    {
                        Model = sourceModel,
                        StoreItemCollection = sourceModel.GetStoreItemCollection(out providerInfo),
                        ProviderManifest = GetProviderManifest(providerInfo),
                        ProviderInfo = providerInfo
                    };
  
          _target
              = new ModelMetadata
                    {
                        Model = targetModel,
                        StoreItemCollection = targetModel.GetStoreItemCollection(out providerInfo),
                        ProviderManifest = GetProviderManifest(providerInfo),
                        ProviderInfo = providerInfo
                    };
  
          _consistentProviders
              = _source.ProviderInfo.ProviderInvariantName.EqualsIgnoreCase(
                  _target.ProviderInfo.ProviderInvariantName)
                &amp;&amp; _source.ProviderInfo.ProviderManifestToken.EqualsIgnoreCase(
                    _target.ProviderInfo.ProviderManifestToken);
  
          var renamedColumns = FindRenamedColumns().ToList();
          var addedColumns = FindAddedColumns(renamedColumns).ToList();
          var alteredColumns = FindChangedColumns().ToList();
          var removedColumns = FindRemovedColumns(renamedColumns).ToList();
          var renamedTables = FindRenamedTables().ToList();
          var movedTables = FindMovedTables().ToList();
          var addedTables = FindAddedTables(renamedTables).ToList();
  
          var columnNormalizedSourceModel = BuildColumnNormalizedSourceModel(renamedColumns);
  
          var addedForeignKeys = FindAddedForeignKeys(columnNormalizedSourceModel).ToList();
          var removedTables = FindRemovedTables(renamedTables).ToList();
          var removedForeignKeys = FindRemovedForeignKeys(columnNormalizedSourceModel).ToList();
          var changedPrimaryKeys = FindChangedPrimaryKeys(columnNormalizedSourceModel).ToList();
  
          if (includeSystemOperations == null)
          {
              includeSystemOperations
                  = sourceModel.HasSystemOperations() &amp;&amp; targetModel.HasSystemOperations();
          }
  
          return renamedTables
              .Concat&lt;MigrationOperation&gt;(movedTables)
              .Concat(removedForeignKeys)
              .Concat(removedForeignKeys.Select(fko =&gt; fko.CreateDropIndexOperation()))
              .Concat(renamedColumns)
              .Concat(addedTables)
              .Concat(addedColumns)
              .Concat(alteredColumns)
              .Concat(changedPrimaryKeys)
              .Concat(addedForeignKeys.Select(fko =&gt; fko.CreateCreateIndexOperation()))
              .Concat(addedForeignKeys)
              .Concat(removedColumns)
              .Concat(removedTables)
              .Where(o =&gt; (includeSystemOperations == true) || !o.IsSystem)
              .ToList();
      }
  </code></pre>
  
  <p>I don't know why, and I can't go ahead in front at this error. Anybody has an idea about how to fix this problem?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've implemented the <code>Generate(InsertHistoryOperation insertHistoryOperation)</code> method to add the migrations history like this:</p>
  
  <pre><code>protected override void Generate(InsertHistoryOperation insertHistoryOperation)
      {
  
          using (var writer = Writer())
          {
  
              writer.Write("INSERT INTO \"dbo\".\"__MigrationHistory\" (\"MigrationId\", \"Model\", \"ProductVersion\") VALUES ('");
  
              writer.Write(insertHistoryOperation.MigrationId + "old");
  
              writer.Write("',E'");
  
              writer.Write("x0" + insertHistoryOperation.Model.ToHexString());
  
              writer.Write("','");
  
              writer.Write(insertHistoryOperation.ProductVersion);
  
              writer.Write("')");
  
              Statement(writer);
  
          }
  
      }
  </code></pre>
  
  <p>The database seems to be created perfectly, but the error is still taking me to pulling my hair. Am I saving the Model in a wrong way?</p>
  
  <p>The error should be raised in this routine on the EdmModelDiffer.cs of EF trying converting the MaxLength to int.</p>
  
  <pre><code>private static ColumnModel BuildColumnModel(
          XElement property, string entitySetName, ModelMetadata modelMetadata)
      {
          Contract.Requires(property != null);
          Contract.Requires(!string.IsNullOrWhiteSpace(entitySetName));
          Contract.Requires(modelMetadata != null);
  
          var nameAttribute = property.NameAttribute();
          var nullableAttribute = property.NullableAttribute();
          var maxLengthAttribute = property.MaxLengthAttribute();
          var precisionAttribute = property.PrecisionAttribute();
          var scaleAttribute = property.ScaleAttribute();
          var storeGeneratedPatternAttribute = property.StoreGeneratedPatternAttribute();
          var storeType = property.TypeAttribute();
  
          var entityType
              = modelMetadata.StoreItemCollection
                  .OfType&lt;EntityType&gt;()
                  .Single(et =&gt; et.Name.EqualsIgnoreCase(entitySetName));
  
          var edmProperty
              = entityType.Properties[nameAttribute];
  
          var typeUsage = modelMetadata.ProviderManifest.GetEdmType(edmProperty.TypeUsage);
  
          var defaultStoreTypeName = modelMetadata.ProviderManifest.GetStoreType(typeUsage).EdmType.Name;
  
          var column
              = new ColumnModel(((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind, typeUsage)
                    {
                        Name = nameAttribute,
                        IsNullable
                            = !string.IsNullOrWhiteSpace(nullableAttribute)
                              &amp;&amp; !Convert.ToBoolean(nullableAttribute, CultureInfo.InvariantCulture)
                                  ? false
                                  : (bool?)null,
                        MaxLength
                            = !string.IsNullOrWhiteSpace(maxLengthAttribute)
                                  ? Convert.ToInt32(maxLengthAttribute, CultureInfo.InvariantCulture)
                                  : (int?)null,
                        Precision
                            = !string.IsNullOrWhiteSpace(precisionAttribute)
                                  ? Convert.ToByte(precisionAttribute, CultureInfo.InvariantCulture)
                                  : (byte?)null,
                        Scale
                            = !string.IsNullOrWhiteSpace(scaleAttribute)
                                  ? Convert.ToByte(scaleAttribute, CultureInfo.InvariantCulture)
                                  : (byte?)null,
                        StoreType
                            = !storeType.EqualsIgnoreCase(defaultStoreTypeName)
                                  ? storeType
                                  : null
                    };
  
          column.IsIdentity
              = !string.IsNullOrWhiteSpace(storeGeneratedPatternAttribute)
                &amp;&amp; storeGeneratedPatternAttribute.EqualsIgnoreCase("Identity")
                &amp;&amp; _validIdentityTypes.Contains(column.Type);
  
          Facet facet;
          if (typeUsage.Facets.TryGetValue(DbProviderManifest.FixedLengthFacetName, true, out facet)
              &amp;&amp; facet.Value != null
              &amp;&amp; (bool)facet.Value)
          {
              column.IsFixedLength = true;
          }
  
          if (typeUsage.Facets.TryGetValue(DbProviderManifest.UnicodeFacetName, true, out facet)
              &amp;&amp; facet.Value != null
              &amp;&amp; !(bool)facet.Value)
          {
              column.IsUnicode = false;
          }
  
          var isComputed
              = !string.IsNullOrWhiteSpace(storeGeneratedPatternAttribute)
                &amp;&amp; storeGeneratedPatternAttribute.EqualsIgnoreCase("Computed");
  
          if ((column.Type == PrimitiveTypeKind.Binary)
              &amp;&amp; (typeUsage.Facets.TryGetValue(DbProviderManifest.MaxLengthFacetName, true, out facet)
                  &amp;&amp; (facet.Value is int)
                  &amp;&amp; ((int)facet.Value == 8))
              &amp;&amp; isComputed)
          {
              column.IsTimestamp = true;
          }
  
          return column;
      }
  </code></pre>