具有MySQL连接器6.4.3自动生成表的实体框架4导致"列长度"异常

AFr*_*eze 12 mysql entity-framework-4 asp.net-mvc-3

我使用Code First方法处理MySQL和.Net EntityFramework 4.mysql连接器版本是6.4.3.

当我第一次运行项目时,我的初始化程序尝试"DropCreateDatabaseAlways".创建数据库以及所有表.然后抛出以下异常.

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

我不确定这个"ModelHash"列是什么,因为它在我的模型中不存在.

谢谢,AFrieze


我已经确定这个ModelHash列的位置,它是用来跟踪变化的EdmMetadata表.通过添加以下modelBuilder.Conventions.Remove();可以消除我遇到的错误.有没有办法将EdmMetadata与MySQL一起使用?

Tz_*_*Tz_ 1

IncludeMetadataConvention 只不过是映射 EdmMetadata 实体。

void IConfigurationConvention.Apply(ModelConfiguration modelConfiguration)
{
    modelConfiguration.Entity(typeof(EdmMetadata), false).ToTable("EdmMetadata");
}
Run Code Online (Sandbox Code Playgroud)

AFAI 看到框架只是在其他地方检查这个类是否被映射并采取相应的行动。

作为解决方法,您可以尝试在映射中映射 EdmMetadata 实体(而不是使用此约定,因此请删除约定),其中您可以使用适当的大小或类型(文本)显式映射 ModelHash 属性。关于“如何映射字段的字段长度或类型”,您可以在此处找到信息:How do Ispecify that a property should generated a TEXT columnrather not an nvarchar(4000)根据那里的答案,这应该更改的类型文本列:

modelBuilder.Entity<EdmMetadata>()
.Property(e => e.ModelHash)
.HasColumnType("text");
Run Code Online (Sandbox Code Playgroud)

有人可以尝试这种方法吗?