Bug #70940 Exception generated with Entity Framework 6 Code First
Submitted: 18 Nov 2013 15:42 Modified: 22 May 2014 17:51
Reporter: Bernard Grosperrin Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:6.8.1.0 beta OS:Windows
Assigned to: Roberto Ezequiel Garcia Ballesteros CPU Architecture:Any

[18 Nov 2013 15:42] Bernard Grosperrin
Description:
Using Code First with Entity Framework 6, I get an exception when EntityFramework wants to add __MigrationHistory primary key:

-- MySql script
-- Created on 18/11/2013 16:31:47
CREATE TABLE `__MigrationHistory`(
        `MigrationId` nvarchar (150) NOT NULL,
        `ContextKey` nvarchar (300) NOT NULL,
        `Model` longblob NOT NULL,
        `ProductVersion` nvarchar (32) NOT NULL);

ALTER TABLE `__MigrationHistory` ADD PRIMARY KEY (MigrationId, ContextKey);

-- Executing at 18/11/2013 16:31:48 +01:00
-- Failed in 107 ms with error: Specified key was too long; max key length is 76
7 bytes

Same code with connector 6.7.4.0 and EF 5 works just fine, but I noticed tables are not identical. For EF5, there is no "ContextKey" column.

How to repeat:
Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>());

forces to recreate the whole DB each time, including migrations.
[18 Nov 2013 17:24] Bernard Grosperrin
see file attached to Bug #70943, it's the same code.
[27 Nov 2013 1:05] Roberto Ezequiel Garcia Ballesteros
Hi,

You need to use MySqlEFConfiguration class in order to load all the resolvers for MySql.

You can add this attribute to your context class:

[DbConfigurationType(typeof(MySqlEFConfiguration))]
  class JourneyContext : DbContext
...

Let us know if this works for you.

Regards,
Roberto
[4 Dec 2013 8:01] Tiger WU
I encountered the same issue, so I tired to add "[DbConfigurationType(typeof(MySqlEFConfiguration))]" to my DbContext, unfortunatelly, when adding a new record, EntityCommandCompilationException occured. The detail of the exception was as below:
Error#2013-12-04 15:56:49: 
Server stack trace: 
   In System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)
   In System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)
   In System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__2()
   In System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__1()
   In System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   In System.Lazy`1.CreateValue()

Exception rethrown at [0]: 
   In System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)
   In System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)
   In System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__2()
   In System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__1()
   In System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   In System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   In System.Lazy`1.CreateValue()
   In System.Lazy`1.LazyInitValue()
   In System.Lazy`1.get_Value()
   In System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   In System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   In System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
   In System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
   In System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   In System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
   In System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   In System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(Func`2 createContext)
   In System.Data.Entity.Internal.InternalContext.QueryForModelHash()
   In System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata)
   In System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata)
   In System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata)
   In System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
   In System.Data.Entity.Internal.InternalContext.<>c__DisplayClass9`1.<CreateInitializationAction>b__8()
   In System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)

Without "[DbConfigurationType(typeof(MySqlEFConfiguration))]", a new object instance can be instered correctly.
[10 Jan 2014 15:31] Roberto Ezequiel Garcia Ballesteros
What MySqlEFConfiguration does to solve this problem is overriding history table operation to create only one column in the primary table instead of two. You can do the same (avoiding using MySqlEFConfiguration) using the following code inside your DbConfiguration class:

SetHistoryContext(MySqlProviderInvariantName.ProviderName, (existingConnection, defaultSchema) => new MySqlHistoryContext(existingConnection, defaultSchema));

Regards,
Roberto
[12 Jan 2014 13:50] Tiger WU
It works, but need to use a class derived  from MySql.Data.Entity.MySqlHistoryContext in the SetHistoryContext statement  in DbConfiguration subclass: "MyDbConfiguration"; otherwise the error "MyDbConfiguration is not in the same assembly as the oneMySqlHistoryContext in" was reported.

Since MySqlEFConfiguration is defined, using this class should have solved all problems directly, to avoid reinventing the wheels.
[22 May 2014 17:51] Roberto Ezequiel Garcia Ballesteros
I'm closing this thread since it was solved using MySqlHistoryContext derived class.