EF4 Code First和EF6 Code First链接mysql的方法

1、首先需要下载安装.Net的Mysql驱动

下载地址如下:http://dev.mysql.com/downloads/connector/net/

2、配置Web.Config

EF6:

配置链接字符串

<connectionStrings>
     <add name="sqldata" connectionString="server=地址;Character Set=utf8;User Id=用户名;password=密码;Persist Security Info=True;database=数据库名称" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

配置驱动

<system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove><!--预防Machine.config中已经配置了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.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

 配置EF6的驱动

<entityFramework>
<providers> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" /> </providers>
</entityFramework>

 

 

EF4.1:

配置链接字符串

<connectionStrings>
     <add name="sqldata" connectionString="server=地址;Character Set=utf8;User Id=用户名;password=密码;Persist Security Info=True;database=数据库名称" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

配置驱动

<system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove><!--预防Machine.config中已经配置了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.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

 

配置完这些就可以使用Mysql了  !



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。