EF for MySql 开发配置手册

执行MySQL安装程序,安装MySQL For Visual Studio和Connector/NET

 

执行命令:

Install-Package EntityFramework

Install-Package MySql.Data -Version 6.9.8

Install-Package MySql.Data.Entity -Version 6.9.8

 

MySQL连接语句:

<connectionStrings>
    <add name="FgwContext" connectionString="server=192.168.7.205;user id=fgw;password=fgw3edc#;persistsecurityinfo=True;database=fgwpro" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>

 

修改Config

 <connectionStrings>
    <add name="Default" connectionString="data source=192.168.7.205;user id=fgw;password=fgw3edc#;database=fgwpro;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="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" />
    </DbProviderFactories>
  </system.data>

 

错误记录:

No MigrationSqlGenerator found for provider 'MySql.Data.MySqlClient'. Use the SetSqlGenerator method in the target migrations configuration class to register additional SQL generators.

 

解决方案:

需要在Context指定Mysql的配置文件。

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]

 

 

中文乱码问题

使用语句 show variables like 'character%'; 来查看当前数据库的相关编码集。

 

client

为客户端使用的字符集。

connection

为连接数据库的字符集设置类型,如果程序没有指明连接数据库使用的字符集类型则按照服务器端默认的字符集设置。

database

为数据库服务器中某个库使用的字符集设定,如果建库时没有指明,将使用服务器安装时指定的字符集设置。

results

为数据库给客户端返回时使用的字符集设定,如果没有指明,使用服务器默认的字符集。

server

为服务器安装时指定的默认字符集设定。

system

为数据库系统使用的字符集设定。

[mysql]

default-character-set=utf8

[mysqld]

character-set-server=utf8

重启MySQL服务即可。

posted @ 2016-08-16 11:24  LiGoper  阅读(1938)  评论(0编辑  收藏  举报