使用Entity Framework同时连接Mysql数据库和SqlServer数据库

  1. 下载并安装Mysql Connector/Net
    下载地址:link
  2. 添加引用
    MySql.Data.Entity.EF6
  3. 修改配置文件App.config或Web.config

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  configSections>
  <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>

  <connectionStrings>
    <add name="SqlServerConnStr"  connectionString="Server=服务器地址;Initial Catalog=数据库名称;User ID=用户名;Password=密码" providerName="System.Data.SqlClient"/>
    <add name="MysqlConnStr"    connectionString="Server=服务器地址;Initial Catalog=数据库名称;User ID=用户名;Password=密码" providerName="MySql.Data.MySqlClient"/>
  connectionStrings>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
  startup>
configuration>

你可能感兴趣的:(C#)