Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4 with MySQL connector 6.4.3 autogenerate tables causes "Column Length" exception

I am working with MySQL and the .Net EntityFramework 4 using the Code First approach. The mysql connector version is 6.4.3.

When I run the project for the first time my initializer attempts to "DropCreateDatabaseAlways". The database is created as well as all the tables. Then the following exception is thrown.

Column length too big for column 'ModelHash' (max = 21845); use BLOB or TEXT instead
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: MySql.Data.MySqlClient.MySqlException: Column length too big for column 'ModelHash' (max = 21845); use BLOB or TEXT instead

Source Error:

Line 38: public virtual List GetAll() Line 39: { Line 40: return dbSet.ToList(); Line 41: } Line 42:

Source File: C:\Users\Andrew\Documents\Visual Studio 2010\Projects\SearchCore\OnlineID.DAL\GlobalGatewayRepository.cs Line: 40

Stack Trace:

[MySqlException (0x80004005): Column length too big for column 'ModelHash' (max = 21845); use BLOB or TEXT instead] MySql.Data.MySqlClient.MySqlStream.ReadPacket() +198 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId) +73 MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId) +20 MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) +100 MySql.Data.MySqlClient.MySqlDataReader.NextResult() +836 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) +1399 MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() +36 MySql.Data.MySqlClient.MySqlScript.Execute() +551 MySql.Data.MySqlClient.MySqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection) +260 System.Data.Objects.ObjectContext.CreateDatabase() +84 System.Data.Entity.Internal.DatabaseOperations.CreateIfNotExists(ObjectContext objectContext) +28 System.Data.Entity.Database.CreateIfNotExists() +53 System.Data.Entity.DropCreateDatabaseAlways1.InitializeDatabase(TContext context) +233 System.Data.Entity.<>c__DisplayClass21.<SetInitializerInternal>b__0(DbContext c) +75 System.Data.Entity.Internal.<>c__DisplayClass5.<PerformDatabaseInitialization>b__3() +19 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72 System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +169 System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +7 System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) +118 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action1 action) +190 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +27 System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +62 System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator() +15 System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable.GetEnumerator() +40 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable1 source) +58 OnlineID.DAL.GlobalGatewayRepository1.GetAll() in C:\Users\Andrew\Documents\Visual Studio 2010\Projects\SearchCore\OnlineID.DAL\GlobalGatewayRepository.cs:40 OnlineID.BAL.AccountService.GetAccounts() in C:\Users\Andrew\Documents\Visual Studio 2010\Projects\SearchCore\OnlineID.BAL\AccountService.cs:32 OnlineID.Website.Controllers.AccountManagement.AccountManagementController.Index() in C:\Users\Andrew\Documents\Visual Studio 2010\Projects\SearchCore\OnlineID.Website\Controllers\AccountManagement\AccountManagementController.cs:29 lambda_method(Closure , ControllerBase , Object[] ) +62 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +208 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +27 System.Web.Mvc.<>c_DisplayClass15.b_12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters) +191 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 System.Web.Mvc.Controller.ExecuteCore() +116 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 System.Web.Mvc.Async.<>c__DisplayClass81.b__7(IAsyncResult ) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.<>c_DisplayClasse.b_d() +50 System.Web.Mvc.SecurityUtil.b_0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

I'm not sure what this "ModelHash" column is as it does not exist in my models.

Thanks, AFrieze


I have identified the location of this ModelHash column, it is in the EdmMetadata table that is used to track changes. The error I am experiencing can be eliminated by adding the following modelBuilder.Conventions.Remove(); Is there a way to use EdmMetadata with MySQL then?

like image 457
AFrieze Avatar asked Jul 06 '11 20:07

AFrieze


1 Answers

The IncludeMetadataConvention does nothing more than it maps the EdmMetadata entity.

void IConfigurationConvention.Apply(ModelConfiguration modelConfiguration)
{
    modelConfiguration.Entity(typeof(EdmMetadata), false).ToTable("EdmMetadata");
}

AFAI see the framework just checks in other places if this class is mapped or not and acts accordingly.

As a workaround you could try to map the EdmMetadata entity in your mapping (instead of using this convention for that, so keep the convention removed), where you could explicitly map the ModelHash property with an appropriate size or type (text). On "how to map the field length or type of a field" you can find info here: How do I specify that a property should generate a TEXT column rather than an nvarchar(4000) According to the answers there this should change the type of the column to text:

modelBuilder.Entity<EdmMetadata>()
.Property(e => e.ModelHash)
.HasColumnType("text");

Can someone try this approach?

like image 191
Tz_ Avatar answered Sep 19 '22 06:09

Tz_