【问题标题】:Why can't I update database using ef6 code first mysql migrations? [closed]为什么我不能使用 ef6 代码先 mysql 迁移更新数据库? [关闭]
【发布时间】:2019-03-16 08:38:48
【问题描述】:

在使用 ef6 连接到 Godaddy 上的 MySql 数据库时首先实现代码时遇到问题。它仍在尝试连接到 SQL 实例

运行update-database时出现以下错误

错误号:53,状态:0,类:20 建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)

这是我的 app.config:

<xml version="1.0" encoding="utf-8">
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="LMScontext" connectionString="Server=servername;Database=dbname;Uid=user;Pwd=password;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <entityFramework>

    <providers>
      <!--<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />-->
      <!--<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.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
    </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.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
</configuration>

【问题讨论】:

  • 这是数据库连接错误,说明您的连接字符串不正确或未指定。您必须检查服务器名称、数据库名称、用户名和密码是否正确。

标签: c# mysql entity-framework entity-framework-6


【解决方案1】:

在我的情况下可行的一种可能的解决方案是,

在&lt;providers&gt;.....&lt;/providers&gt;上方的行下方添加

<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
<providers>
  ...
</providers>

然后从web.config 或app.config 中删除&lt;DbProviderFactories&gt;.....&lt;/DbProviderFactories&gt; 部分。

然后将以下属性添加到DbContext 类实现

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]

这样看起来,

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 
public class MySqlDbContext : DbContext 
{
    .....
}

【讨论】:

    【解决方案2】:

    试试this。我可能觉得会有所帮助

    【讨论】:

    • 在 SO 上不接受仅链接的答案,请在答案中包含相关文本
    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多