Entity Framework连接Mysql数据库并生成Model和DAL层
一,安装:
1、开发环境: VS2013与EF6
2、Mysql数据库为:Mysql Server 6.0
3、安装:Mysql for Visual Studio 1.1.1
4、安装 Mysql Connector/Net 6.8.3 GA
二,引用dll:
1、采用Nuget安装EF6.0.2;
2、采用Nuget安装MySql.Data.Entity.EF6
注意:要采用Nuget进行安装,否则可能会缺少相应的dll或者是配置信息
三、配置 web.config或app.config
1、将entitframework节点替代为:
1
2
3
4
5
6
7
|
<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 invariantName= "System.Data.SqlClient" type= "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> |
2、添加 ConnectionString节点:
1
2
3
|
< connectionStrings > < add name = "MyContext" connectionString = "Data Source=localhost;port=3306;Initial Catalog=数据库名称;user id=Mysql的登录用户名;password=Mysql server密码;" providerName = "MySql.Data.MySqlClient" /> </ connectionStrings > |