mysql - "The provider did not return a ProviderManifestToken string"带有 Entity Framework 的 MySQL

标签 mysql entity-framework-6

我在 VS 2017 中建立了一个新项目。我打算使用 EF CodeFirst 方法。到目前为止,我使用的是 Azure SQL 数据库,而在这个测试项目中,我想使用我的远程 MySQL 数据库。我已经创建了用户并且权限设置得恰到好处。我可以通过 MySQL Workbench 访问这个远程数据库服务器。

我创建了一个新的空白 MVC 项目,并通过 Nuget 安装了 MySQL.Data.Entity(版本 6.9.10)。

我的上下文类:

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class WebDb : DbContext
{
    public WebDb() : base("WebDb")
    {

    }

    public DbSet<Candidate> Candidates { get; set; }
}

我的 web.config 有这些条目:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove 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.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>

我的连接字符串是:

<connectionStrings>
    <add name="WebDb" providerName="MySql.Data.MySqlClient" connectionString="server=x.x.x.x;uid=dbuser;pwd=password;database=temp1;" />
</connectionStrings>

我有一个简单的领域类

public class Candidate
{
    public int Id { get; set; }
    public string Name { get; set; }
}

当我给出 Enable-Migrations -Force 命令时,我得到

Checking if the context targets an existing database... System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. at MySql.Data.MySqlClient.NativeDriver.Open() at MySql.Data.MySqlClient.Driver.Open() at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at MySql.Data.MySqlClient.MySqlPool.GetConnection() at MySql.Data.MySqlClient.MySqlConnection.Open() at MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) --- End of inner exception stack trace --- at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) at MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection) at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()<br/> at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized() at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer) at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w) at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action1 writeXml) at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration) at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() The provider did not return a ProviderManifestToken string.

据我所知,我进行了广泛的搜索,但无济于事。这里发生了什么事?我错过了什么?

最佳答案

我最近遇到了和你一样的问题。在 SQL Server 中一切正常,但我在转换为 MySQL 时遇到了很多问题。对我有用的一些事情是:

    Install-Package MySQL.Data -Version 6.9.9
    Install-Package MySql.Data.Entity -Version 6.9.10

较新的 8.0 版 MySQL 包似乎有问题。当我恢复到旧版本时,它起作用了。

您的 app.config 应如下所示:

      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.9.9.0" newVersion="6.9.9.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove 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.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
      <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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
</configuration>

即使在那之后,我才发现 MySQL 中存在一些差异。例如,您的迁移“索引”语句将不起作用。您必须自己编辑迁移文件并构建索引。另一件我刚刚读到但没有遇到的事情是 MySQL 驱动程序不允许多个连接,这可能意味着改变您检索异步集合的方式。最后,我遇到了 RowVersion 是 byte[] 的问题。我使用了以下文章的一个变体来解决这个问题(希望如此!)。

Better way to implement a row version with EF Core and MySQL?

关于mysql - "The provider did not return a ProviderManifestToken string"带有 Entity Framework 的 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353585/

相关文章:

c# - Firebird embedded 和 Entity Framework 6 出现 "Unknown data type"错误

entity-framework - 首先为代码配置 EF 和 Npgsql

c# - 将 WithOptional 转换为 Entity Framework Core(7) 等效项

mysql - 使用某些逻辑无法创建mysql事件

php - 是否可以通过内连接查询Mysql

php - MySQL 函数在 PHP 中返回难以使用的结果

.net - Entity Framework 6.1.3 中的 Citext 数据类型

mysql - 由于在 MySQL 中使用保留字作为表名或列名导致的语法错误

PHP 混淆了 ä、ö、ü 等字母

c# - 无法通过 Visual Studio 2013 中的 EF6 连接到 MySQL