【问题标题】:Change existing .NET Entity Framework layer from Sql Server to MySql将现有的 .NET Entity Framework 层从 Sql Server 更改为 MySql
【发布时间】:2015-09-28 06:00:27
【问题描述】:

问题:我有一个使用 .NET Entity Framework 库来访问 Sql Server 数据库的 Web 应用程序。我应该怎么做才能将其更改为 MySQL ?

更多细节

我尝试更改 app.config 中的参数,这是我的 app.config 文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="mysql_db" connectionString="server=localhost;user id=root;password=;database=foo" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <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.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
     </providers>
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>  

我在使用上述文件后运行了应用程序,但出现以下错误。

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

我还应该进行哪些更改以使其使用 MySQL 数据库。我已经安装了必要的工具(MySQL、.NET MySQL Connector、MySQL for Visual Studio)。

【问题讨论】:

    标签: c# mysql entity-framework


    【解决方案1】:

    我认为您应该安装 MySQL Connector/Net 并配置正确的连接字符串(这是一个示例) Here you can find doc x ef6

            <connectionStrings>
                <add name="MyContext" providerName="MySql.Data.MySqlClient" 
                    connectionString="server=localhost;port=3306;database=mycontext;uid=root;password=********"/>
            </connectionStrings>
            <entityFramework>
                <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, 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"/>
                </providers>
            </entityFramework>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-23
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多