首页 > 代码库 > 使用EF连接MySQL数据库

使用EF连接MySQL数据库

使用Nuget加载包:EntityFramework,MySql.Data,MySql.Data.Entity
检查App.config或是web.config相关节点
1. EF配置节点
 1 <configSections>
 2     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 3 </configSections>
 4 <entityFramework>
 5     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
 6       <parameters>
 7         <parameter value=http://www.mamicode.com/"mssqllocaldb" />
 8       </parameters>
 9     </defaultConnectionFactory>
10     <providers>
11       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
12     <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers>
13 </entityFramework>

 

2. MySQL配置节点
1  <system.data>
2     <DbProviderFactories>
3       <remove invariant="MySql.Data.MySqlClient" />
4       <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.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
5     </DbProviderFactories>
6  </system.data>

 

3. 连接字符串
1 <connectionStrings>
2     <add name="NorthwindConnection" connectionString="Data Source=localhost;port=3306;Initial Catalog=northwind;user id=developer;password=developer;"
3          providerName="MySql.Data.MySqlClient" />
4 </connectionStrings>

 

使用EF连接MySQL数据库