Bug #62135 Connector/Net Incorrectly Maps PrimitiveTypeKind.Byte to "tinyint"
Submitted: 10 Aug 2011 0:33 Modified: 6 Oct 2011 20:39
Reporter: Alexander Nagy Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.4.3 OS:Windows
Assigned to: Gabriela Martinez Sanchez CPU Architecture:Any

[10 Aug 2011 0:33] Alexander Nagy
Description:
Connector/Net is mapping Entity Framework PrimitiveTypeKind.Byte to "tinyint".  This is incorrect because tinyint is a signed type and Byte is an unsigned type.  The correct mapping is "utinyint".

Additionally, Connector/Net does not map PrimitiveTypeKind.SByte at all.

How to repeat:
Create a simple Code-First type with a Byte for a field:

    public class Dog
    {
        [Key]
        public virtual byte Oops { get; set; }
    }

    public class DogDBContext : DbContext
    {
        public DbSet<Dog> Dogs { get; set; }
    }

Upon first loading the type, the following exception is generated:

System.Data.MappingException occurred
  Message=Schema specified is not valid. Errors: 
(7,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Byte[Nullable=False,DefaultValue=]' of member 'Oops' in type 'MySql.Data.Entity.ModelFirst.Tests.Dog' is not compatible with 'MySql.tinyint[Nullable=False,DefaultValue=]' of member 'Oops' in type 'CodeFirstDatabaseSchema.Dog'.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Mapping.StorageMappingItemCollection.Init(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable`1 xmlReaders, List`1 filePaths, Boolean throwOnError)
       at System.Data.Mapping.StorageMappingItemCollection..ctor(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable`1 xmlReaders)
       at System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.ToStorageMappingItemCollection(DbDatabaseMapping databaseMapping, EdmItemCollection itemCollection, StoreItemCollection storeItemCollection)
       at System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping, Action`1 storeAction)
       at System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace..ctor(DbDatabaseMapping databaseMapping)
       at System.Data.Entity.Infrastructure.DbCompiledModel..ctor(DbModel model)
       at System.Data.Entity.Infrastructure.DbModel.Compile()
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       at System.Data.Entity.Internal.InternalContext.ExecuteSqlCommand(String sql, Object[] parameters)
       at System.Data.Entity.Database.ExecuteSqlCommand(String sql, Object[] parameters)
       at D:\Users\optimiz3\Documents\MySql\connectornet\trunk\Tests\MySql.Data.Entity.ModelFirst.Tests\CodeFirstTests.cs:line 69
  InnerException: 

Suggested fix:
ProviderManifest.cs lines 128-130 need to be changed as follows:

Old:

                case PrimitiveTypeKind.Byte:
                    return TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]); // ERROR

Fixed:

                case PrimitiveTypeKind.SByte:
                    return TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]);

                case PrimitiveTypeKind.Byte:
                    return TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["utinyint"]);
[6 Oct 2011 20:39] Gabriela Martinez Sanchez
Fixed in versions 6.3+ and 6.4+
[10 Oct 2011 19:22] Philip Olson
This is fixed as of 6.4.5:

+        Connector/NET incorrectly maps
+        <literal>PrimitiveTypeKind.Byte</literal> to
+        <literal>tinyint</literal>, instead of
+        <literal>utinyint</literal>. And
+        <literal>PrimitiveTypeKind.SByte</literal> mapping was added, to
+        <literal>tinyint</literal>.