In the previous version this functionality was working fine. Probably with the introduction with EF migrations this bug was introduced (looking at the stacktrace). We have a property of type byte[] which we store as a varbinary, in the configuration class of the entity we use the following line:
this.Property(m => m.CustomProperty).IsRequired().HasColumnType("VarBinary").IsMaxLength(); //ERROR
this.Property(m => m.CustomProperty).IsRequired().HasColumnType("VarBinary").HasMaxLength(8000)(); //NO ERROR
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.<>c__DisplayClass125.<BuildCreateTableOperation>b__123(XElement p)
at System.Data.Entity.Migrations.Extensions.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildCreateTableOperation(String entitySetName, String tableName, String schema, ModelMetadata modelMetadata)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<FindAddedTables>b__31(XElement es)
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.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.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()
at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
at System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext)
at System.Data.Entity.Database.CreateIfNotExists()
at ANAAC.TCC.Fractal.Data.Dialogs.OpenConnectionDialog.CreateDatabase(String path) in
The error pops up for both SQL compact and SQL Server databases.
Because we dont have VS2012 we could not run the ef project, but looking at the code we think that the problem lies in the following class:
-class:EdmModelDiffer -> method:BuildColumnModel ->
= 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,
Environment:
- Windows XP
- VS2010
- .Net 4.0
- SQL Compact 4.0 SP1
Kind regards,
W. Kosten
Comments: This applies not only to VarBinary but also to VarChar and NVarChar fields.