Bug #63920 Database generation failed if using class inheritance with Entity Framework
Submitted: 4 Jan 2012 13:41 Modified: 28 Sep 2012 18:37
Reporter: Tyron Scholem Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.4.4, 6.5.0 (beta) OS:Any
Assigned to: Roberto Ezequiel Garcia Ballesteros CPU Architecture:Any
Tags: class inheritance, entity framework, generation script

[4 Jan 2012 13:41] Tyron Scholem
Description:
If I code a model with class inheritance, Entity Framework (with Code-First approach) won't be able to create the database using MySQL Connector. It just throws an error saying:

Schema specified is not valid. Errors: 
(25,6) : error 0064: Facet 'MaxLength' must not be specified for type 'mediumtext'.

I guess this error has something to do with the "Discriminator" column that EF try to create.

If I try to use Table-Per-Hierarchy (i.e. create a single table for each of the inherited models), I receive another error:

Can't create table 'aimpa.#sql-da8_2c' (errno: 150)

How to repeat:
Create the models:
    public abstract partial class Vehicle
    {
        public int Id { get; set; }
        public int Year { get; set; }
    }
    
    public partial class Car : Vehicle
    {
        public string CarProperty { get; set; }
    }
    
    public partial class Bike : Vehicle
    {
        public string BikeProperty { get; set; }
    }

----------

Attempt #1 (without table-per-hierarchy):
- set the context to:

    public class Db : DbContext
    {
        public DbSet<Vehicle> Vehicles { get; set; }
    }

- compile the code, and see the results

----------

Attempt #2 (with table-per-hierarchy):
- set the context to:

    public class Db : DbContext
    {
        public DbSet<Vehicle> Vehicles { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Vehicle>()
                .Map<Car>(o => o.ToTable("Cars"))
                .Map<Bike>(o=>o.ToTable("Bikes"));
        }
    }

- compile the code
[4 Jan 2012 15:36] Tyron Scholem
Update to the version (reflect that the problem is not only with the beta version).
[30 Mar 2012 15:24] Olag Ulga
@Tyron Scholem: Could you please paste the stacktrace of your exception? Thanks
[30 Mar 2012 16:55] Tyron Scholem
For attempt 1 (without table-per-hierarchy):

   at System.Data.Metadata.Edm.StoreItemCollection.Loader.ThrowOnNonWarningErrors()
   at System.Data.Metadata.Edm.StoreItemCollection.Loader.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths)
   at System.Data.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerManifestToken, Memoizer`2& cachedCTypeFunction)
   at System.Data.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders)
   at System.Data.Entity.ModelConfiguration.Edm.Db.DbDatabaseMetadataExtensions.ToStoreItemCollection(DbDatabaseMetadata database)
   at System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping)
   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.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at BugMysql.Controllers.HomeController.Index() in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\BugMysql\BugMysql\Controllers\HomeController.cs:line 17
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
[30 Mar 2012 16:57] Tyron Scholem
For attempt 2 (with table-per-hierarchy):

   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId)
   at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at MySql.Data.MySqlClient.MySqlScript.Execute()
   at MySql.Data.MySqlClient.MySqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   at System.Data.Objects.ObjectContext.CreateDatabase()
   at System.Data.Entity.Internal.DatabaseOperations.Create(ObjectContext objectContext)
   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.Create()
   at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context)
   at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
   at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6()
   at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
   at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
   at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at BugMysql.Controllers.HomeController.Index() in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\BugMysql\BugMysql\Controllers\HomeController.cs:line 17
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
[11 May 2012 10:59] Olag Ulga
This bug is related to http://bugs.mysql.com/bug.php?id=64288
[15 Jul 2012 15:46] Tyron Scholem
It's been 6 months since this issue was reported. Any solution?
[15 Jul 2012 15:48] Tyron Scholem
I'd rather categorize this as a S1 Critical, since this is the reason I'm not using MySQL on my projects with .NET.
[15 Jul 2012 21:08] Peter Chapman
I agree with Tyron about the severity of this bug - I was forced to switch database engines to Microsoft SQL for a project to workaround this bug.
[18 Jul 2012 19:23] Anderson Fortaleza
This serious bug was informed on 6.4.4 and MySql.Data is on 6.5.4 already and it has not been fixed, I've downloaded the latest version hoping that it would work. Amazing.
[18 Jul 2012 19:46] Roberto Ezequiel Garcia Ballesteros
Hi!

The fix is done and will be available on next releases: 6.4.6, 6.5.5 and 6.6

Regards.
[14 Sep 2012 16:45] Olag Ulga
the bug is known for more than 8 month now an there is still no released fix available...
[14 Sep 2012 17:07] Fernando Gonzalez.Sanchez
This should be fixes in all 6.6.x, starting 6.6.0 (current one is 6.6.2), see http://dev.mysql.com/downloads/connector/net/, Development Releases.

So you still see the bug in 6.6.2 (6.6 Beta 1)?
[14 Sep 2012 22:39] Tyron Scholem
I didn't see the new version before.
I've tested with Connector/Net 6.6.2 beta and works like a charm. Even though I didn't test in a full application yet, this simple example runs fine.
[16 Sep 2012 13:18] Olag Ulga
Sorry for the confusion. In our projects we are not allowed to use developement / beta releases. Because of this we have to wait until there is a GA-Release available.
[28 Sep 2012 18:37] John Russell
Added to changelog for 6.4.6, 6.5.5, 6.6.3: 

When using the Entity Framework Code First approach, the generated
code could be use the MEDIUMTEXT data type in contexts where other
types such as VARCHAR were more appropriate, leading to errors such
as:

error 0064: Facet 'MaxLength' must not be specified for type 'mediumtext'.