简体   繁体   English

EF6代码优先+ MySql迁移:不支持nvarchar(max)类型

[英]EF6 codefirst + MySql migrations: type nvarchar(max) not supported

I'm trying to use EF6 code first + MySql with migrations, but I'm stuck with this error during add-migration : 我尝试先使用EF6代码+ MySql进行迁移,但是在add-migration期间遇到此错误:

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

It thus seems EF is trying to use SQLServer types for MySql, even if I have changed the default connection factory in my config. 因此,即使我已更改配置中的默认连接工厂,似乎EF仍在尝试对MySql使用SQLServer类型。

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? 这是在MySql的DAL dll中启用迁移的步骤,有人可以提供提示吗?

(1) Add the NuGet packages : (1)添加NuGet 软件包

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

(2) Add a connection string for MySql in the App.config file, like: (2)在App.config文件中为MySql添加一个连接字符串 ,例如:

(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) Also, ensure that the configuration (in my case App.config ) has been modified by NuGet as specified: (3)另外,请确保配置 (在我的情况下为App.config )已由NuGet按指定进行了修改:

  • replace the default connection factory: 替换默认的连接工厂:

    (defaultconnectionfactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" /) (默认连接工厂类型=“ MySql.Data.Entity.MySqlConnectionFactory,MySql.Data.Entity.EF6” /)

  • add the MySql provider: 添加MySql提供程序:

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

  • add provider to system.data: 将提供程序添加到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) (system.data)(dbproviderfactories)(删除名称=“ MySQL Data Provider” invariant =“ MySql.Data.MySqlClient” /)(添加名称=“ MySQL Data Provider” invariant =“ MySql.Data.MySqlClient” description =“。适用于MySQL的Net Framework数据提供程序”“ type =” MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data,Version = 6.8.3.0,Culture = neutral,PublicKeyToken = c5687fc88969c44d“ /)(/ dbproviderfactories)(/system.data)

(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 : (4)按照http://dev.mysql.com/doc/connector-net/en/的建议,手动添加代码配置类型属性(我不希望触摸我的数据上下文代码,因为它必须保持通用)。 connector-net-entityframework60.html

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

A couple of string properties in my POCO objects have no max length as they must be nvarchar(max) ie text in MySql. 我的POCO对象中的几个字符串属性没有最大长度,因为它们必须是nvarchar(max)即MySql中的text 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)'.

Any hint? 有什么提示吗?

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. 该修复程序是违反直觉的,但到目前为止效果很好。似乎该问题与使用LocalDB(SQL Server)设置的旧迁移有关,但即使删除旧迁移也没有效果。 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... 我还花了很多不必要的时间来摆弄我的Web.config,数据库上下文等。我建议您尝试以下操作,然后再敲墙。

  1. Delete the migrations folder 删除迁移文件夹
  2. Try enabling migrations and adding your initial migration (in the PM type "enable-migrations" and then "add-migration init") 尝试启用迁移并添加您的初始迁移(在PM类型中,“ enable-migrations”,然后“ add-migration init”)
  3. If you get the above error in the PM, delete the migrations folder again, then open up MySQL Workbench 如果您在PM中遇到上述错误,请再次删除migrations文件夹,然后打开MySQL Workbench
  4. Within MySQL Workbench, locate the Schema with the name you gave in your connectionString (will look like database= whateverYouCalledIt ) 在MySQL Workbench中,找到具有您在connectionString中输入的名称的Schema(看起来像database = whatYouCalledIt
  5. In that schema, open the tables and find "__migrationhistory" 在该架构中,打开表并找到“ __migrationhistory”
  6. Right click that table, click select rows, and delete any existing migration entries, then repeat step 2 右键单击该表,单击选择行,然后删除任何现有的迁移条目,然后重复步骤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... 显然,这并没有删除数据库中的迁移历史记录...

The following steps resulted in the same error message for me: 以下步骤为我导致了相同的错误消息:

  1. Create a project using entity framework and SQL Server, with a migration. 使用实体框架和SQL Server进行迁移来创建项目。 Use some string properties without MaxLength in the model classes, which are mapped to nvarchar(max) . 在模型类中使用一些没有MaxLength string属性,这些属性映射到nvarchar(max)
  2. Reconfigure for MySQL like above, for an empty MySQL database. 如上所述,为一个空的MySQL数据库重新配置MySQL。
  3. Run Update-Database . 运行Update-Database

Using all the latest software versions libraries at moment of writing. 在撰写本文时,请使用所有最新的软件版本库。

After getting the error I tried setting [MaxLength(..)] on all string properties. 收到错误后,我尝试在所有字符串属性上设置[MaxLength(..)] The error message kept coming up, even at recreation of the migration with Add-Migration Initial -Force . 即使使用Add-Migration Initial -Force重新进行迁移时,错误消息也会不断出现。

In my case it turned out that the call to Update-Database on my MySQL database (which ran the old migrations meant for SQL Server including nvarchar(max) ) had resulted in some effects on my database, ie tables being created, but not all. 以我为例,事实证明,对MySQL数据库上的Update-Database的调用(运行了旧的SQL Server迁移,包括nvarchar(max) )对我的数据库产生了一些影响,即创建了表,但不是全部。

The solution for me was: 我的解决方案是:

  1. Drop these tables, make sure the database was empty again. 删除这些表,确保数据库再次为空。
  2. Enable-Migrations -Force
  3. Add-Migration Initial -Force
  4. Update-Database

Note that these steps overwrite any manual changes to your migrations and Seed() function, you might want to restore those selectively. 请注意,这些步骤将覆盖对migrations和Seed()函数的任何手动更改,您可能希望有选择地还原它们。

After this I tested string properties without [MaxLength(..)] . 之后,我测试了没有[MaxLength(..)]字符串属性。 These can be used just fine with the MySQL provider, they are mapped to longtext . 这些可以与MySQL提供程序一起很好地使用,它们被映射到longtext

I had the same problem and found the root cause was existing migrations that looked like this: AddColumn("dbo.RoundJumperMap", "VideoUrl", c => c.String()); 我遇到了同样的问题,发现根本原因是现有的迁移看起来像这样: AddColumn("dbo.RoundJumperMap", "VideoUrl", c => c.String());

Once I found all those and added a maxlength everything ran great. 找到所有这些内容并添加maxlength后,一切就很好了。 AddColumn("dbo.RoundJumperMap", "VideoUrl", c => c.String(maxLength: 1000));

I did not need to delete any of my existing migrations or anything like that. 我不需要删除任何现有的迁移或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM