2015-09-28 60 views
-2

问题:我有一个使用.NET Entity Framework库访问Sql Server数据库的Web应用程序。我应该怎么做才能将其更改为MySQL?将现有的.NET Entity Framework图层从Sql Server更改为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连接器,用于Visual Studio的MySQL)。

回答

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> 
相关问题