I've disabled automatic migration and I created a new MigrationSqlGenerator for Postgresql:
public Configuration() { AutomaticMigrationsEnabled = false; SetSqlGenerator("Npgsql", new PostgreSqlMigrationSqlGenerator()); }
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:
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& 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.<FindChangedColumns>b__ef(<>f__AnonymousType1d`2 <>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.
I've implemented the Generate(InsertHistoryOperation insertHistoryOperation)
method to add the migrations history like this:
protectedoverridevoid 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);
}
}
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?
The error should be raised in this routine on the EdmModelDiffer.cs of EF trying converting the MaxLength to int.
privatestatic 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<EntityType>()
.Single(et => 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)
&& !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)
&& storeGeneratedPatternAttribute.EqualsIgnoreCase("Identity")
&& _validIdentityTypes.Contains(column.Type);
Facet facet;
if (typeUsage.Facets.TryGetValue(DbProviderManifest.FixedLengthFacetName, true, out facet)
&& facet.Value != null&& (bool)facet.Value)
{
column.IsFixedLength = true;
}
if (typeUsage.Facets.TryGetValue(DbProviderManifest.UnicodeFacetName, true, out facet)
&& facet.Value != null&& !(bool)facet.Value)
{
column.IsUnicode = false;
}
var isComputed
= !string.IsNullOrWhiteSpace(storeGeneratedPatternAttribute)
&& storeGeneratedPatternAttribute.EqualsIgnoreCase("Computed");
if ((column.Type == PrimitiveTypeKind.Binary)
&& (typeUsage.Facets.TryGetValue(DbProviderManifest.MaxLengthFacetName, true, out facet)
&& (facet.Value isint)
&& ((int)facet.Value == 8))
&& isComputed)
{
column.IsTimestamp = true;
}
return column;
}