gpt4 book ai didi

mysql - 带有 MySQL 连接器 6.4.3 的 Entity Framework 4 自动生成表导致 "Column Length"异常

转载 作者:IT老高 更新时间:2023-10-29 00:17:45 26 4
gpt4 key购买 nike

我正在使用 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.<p></p>

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

<p>Source Error:</p>

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

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

<p>Stack Trace:</p>

<p>[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, Nullable<code>1 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.DropCreateDatabaseAlways</code>1.InitializeDatabase(TContext context) +233
System.Data.Entity.<>c__DisplayClass2<code>1.<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.RetryAction</code>1.PerformAction(TInput input) +118
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action<code>1 action) +190
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +27
System.Data.Entity.Internal.Linq.InternalSet</code>1.Initialize() +62
System.Data.Entity.Internal.Linq.InternalSet<code>1.GetEnumerator() +15
System.Data.Entity.Infrastructure.DbQuery</code>1.System.Collections.Generic.IEnumerable.GetEnumerator() +40
System.Collections.Generic.List<code>1..ctor(IEnumerable</code>1 collection) +315
System.Linq.Enumerable.ToList(IEnumerable<code>1 source) +58
OnlineID.DAL.GlobalGatewayRepository</code>1.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, IDictionary<code>2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary</code>2 parameters) +27
System.Web.Mvc.<>c_<em>DisplayClass15.b</em>_12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func<code>1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList</code>1 filters, ActionDescriptor actionDescriptor, IDictionary<code>2 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__DisplayClass8</code>1.b__7(IAsyncResult <em>) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c</em>_DisplayClasse.b_<em>d() +50
System.Web.Mvc.SecurityUtil.b</em>_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
</p>

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

谢谢,弗里兹


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

最佳答案

IncludeMetadataConvention 只是映射 EdmMetadata 实体。

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

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

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

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

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

关于mysql - 带有 MySQL 连接器 6.4.3 的 Entity Framework 4 自动生成表导致 "Column Length"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6602480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com