EF6代码优先+ MySql迁移:不支持nvarchar(max)类型 [英] EF6 codefirst + MySql migrations: type nvarchar(max) not supported

查看:86
本文介绍了EF6代码优先+ MySql迁移:不支持nvarchar(max)类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试先使用EF6代码+ MySql进行迁移,但在 add-migration 期间遇到此错误:

I'm trying to use EF6 code first + MySql with migrations, but I'm stuck with this error during add-migration:

The underlying provider does not support the type 'nvarchar(max)'

因此,即使我已更改配置中的默认连接工厂,似乎EF仍在尝试将SQLServer类型用于MySql.

It thus seems EF is trying to use SQLServer types for MySql, even if I have changed the default connection factory in my config.

我可以找到许多具有相同错误消息的问题,但它们似乎都不是最新的,也不是建议我没有尝试过的事情.这是我为MySql在DAL dll中启用迁移的步骤,任何人都可以给出提示吗?

I can find a lot of issues with the same error message but none of them seems up to date or to suggest something I did not already try. Here are my steps for enabling migrations in a DAL dll for MySql, could anyone give a hint?

(1)添加NuGet 软件包:

(1) Add the NuGet packages:

  • MySql.ConnectorNET.Data
  • MySql.ConnectorNET.Entity

(2)在 App.config 文件中为MySql添加连接字符串,例如:

(2) Add a connection string for MySql in the App.config file, like:

(connectionStrings)
(add name="MySqlConn" 
     connectionString="Data Source=127.0.0.1;Port=3306;Database=dummy;User id=***;Password=***;charset=utf8" 
     providerName="MySql.Data.MySqlClient" /)
(/connectionStrings)

(3)另外,请确保NuGet已按指定修改了配置(在我的情况下为 App.config ):

(3) Also, ensure that the configuration (in my case App.config) has been modified by NuGet as specified:

  • 替换默认的连接工厂:

  • replace the default connection factory:

(defaultconnectionfactory type ="MySql.Data.Entity.MySqlConnectionFactory,MySql.Data.Entity.EF6"/)

(defaultconnectionfactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" /)

添加MySql提供程序:

add the MySql provider:

(提供者invariantname ="MySql.Data.MySqlClient"type ="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version = 6.8.3.0,Culture = neutral,PublicKeyToken = c5687fc88969c44d"/)

(provider invariantname="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /)

将提供程序添加到system.data:

add provider to system.data:

(系统数据)(dbproviderfactories)(删除名称="MySQL数据提供程序" invariant ="MySql.Data.MySqlClient"/)(添加名称="MySQL数据提供程序"invariant ="MySql.Data.MySqlClient"description =.Net用于MySQL的Net Framework数据提供程序"type ="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data,Version = 6.8.3.0,Culture = neutral,PublicKeyToken = c5687fc88969c44d"/)(/dbproviderfactories)(/system.data)

(system.data) (dbproviderfactories) (remove name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" /) (add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /) (/dbproviderfactories) (/system.data)

(4)手动添加代码配置类型属性(我不希望触摸我的数据上下文代码,因为它必须保持通用),如

(4) Manually add the code configuration type attribute (I prefer not to touch my data context code, as it must be kept generic), as recommended at http://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html:

(entityframework codeconfigurationtype="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6")

我的POCO对象中的几个字符串属性没有最大长度,因为它们必须是 nvarchar(max),即MySql中的 text .但是,我得到了上面引用的错误,并带有以下堆栈跟踪:

A couple of string properties in my POCO objects have no max length as they must be nvarchar(max) i.e. text in MySql. Yet, I get the error quoted above, with this stacktrace:

System.ArgumentException: The underlying provider does not support the type 'nvarchar(max)'.
   at MySql.Data.MySqlClient.MySqlProviderManifest.GetEdmType(TypeUsage storeType)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildColumnModel(EdmProperty property, ModelMetadata modelMetadata, IDictionary`2 annotations)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildAlterColumnOperation(String table, EdmProperty targetProperty, ModelMetadata targetModelMetadata, EdmProperty sourceProperty, ModelMetadata sourceModelMetadata)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.(FindAlteredColumns)b__24b(()f__AnonymousType2c`2 ()h__TransparentIdentifier243)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion)
   at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.()c__DisplayClass2.(.ctor)b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
The underlying provider does not support the type 'nvarchar(max)'.

有任何提示吗?

推荐答案

我整夜都在为同样的错误而苦苦挣扎.该修复程序是违反直觉的,但到目前为止效果很好.似乎该问题与使用LocalDB(SQL Server)设置的旧迁移有关,但即使删除旧迁移也没有效果.我还花了很多不必要的时间来摆弄我的Web.config,数据库上下文等等.我建议您尝试以下操作,然后再把头砸在墙上...

I struggled with this same error all night. The fix was counter-intuitive but so far so good... It does seem that the issue had to do with old migrations that were set up with LocalDB (SQL Server) yet even deleting the old migrations had no effect. I also spent lots of unnecessary time fiddling with my Web.config, Database Context, etc... I suggest trying the following before banging your head on the wall...

  1. 删除迁移文件夹
  2. 尝试启用迁移并添加您的初始迁移(在PM类型中,"enable-migrations",然后"add-migration init")
  3. 如果在PM中遇到上述错误,请再次删除migrations文件夹,然后打开MySQL Workbench
  4. 在MySQL Workbench中,找到具有您在connectionString中输入的名称的模式(看起来像database = whateverYouCalledIt )
  5. 在该架构中,打开表并找到"__migrationhistory"
  6. 右键单击该表,单击选择行,然后删除所有现有的迁移条目,然后重复步骤2

由于某种原因,这在解决方案资源管理器中删除迁移无效时对我有用.显然,这并没有删除数据库中的迁移历史记录...

For some reason this did the trick for me when deleting migrations in the solution explorer had no effect. Apparently, that didn't delete the migration history in the database...

这篇关于EF6代码优先+ MySql迁移:不支持nvarchar(max)类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆