Reprinted from: http://blog.csdn.net/kmguo/article/details/19650299
There are many online tutorials on using entityframe to connect MySQL database, but many of them are not reliable, reproduced too much. Looked for a long time, finally is configured, now share. , installation: 1, development environment: VS2013 and ef6 2, MySQL database: MySQL Server 6.0 3, installation: MySQL for Visual S Tudio 1.1.1 Download location: https://cdn.mysql.com/Downloads/MySQLInstaller/ mysql-visualstudio-plugin-1.1.1.msi 4, install Mysql connector/net 6.8.3 ga &NBS P Download location:http://dev.mysql.com/downloads/connector/net/ Two, reference dll: 1, install with NuGet ef6.0.2; 2, Install mysql.data.entity.ef6 with NuGet Note: To install with NuGet, you may be missing the appropriate DLL or configuration information III, configuring Web. config or app.config 1, replace the Entitframework node with: <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, add connectionstring node:< connectionstrings> <add name= "mycontext" connectionstring= "Data source=localhost;port=3306;i nitial catalog= database name; user Id=mysql login username; password=mysql server password; providername= "MySql.Data.MySqlClient"/> </connectionStrings> Test C # code
[CSharp]View Plaincopy
- Using System.Data.Entity;
- Namespace Studyef
- {
- public class Mycontext:dbcontext
- {
- Public mycontext (): base ("Name=mycontext")
- {
- }
- Public dbset<data> Datas { get; set;}
- }
- public class Data
- {
- public int Id { get; set;}
- public string Name { get; set;}
- }
- Class Program
- {
- static void Main (string[] args)
- {
- Database.setinitializer (new dropcreatedatabasealways<mycontext> ());
- var context = new Mycontext ();
- Context. Datas.add (new data{name="Ef6-mysql"});
- Context. SaveChanges ();
- }
- }
- }
V. Query under MySQL cmd:
Vi. using Database First: This is done the same way as the database under SQL Server, but sometimes it is not connected to MySQL. I initially tried to connect to MySQL using database, but I couldn't connect to MySQL server in VS2013. That's why I used the code first. But when I succeeded with code frist, I found out that my VS2013 was also able to connect to MySQL server. It's amazing, I don't know why.
Reprint: EntityFramework 6.0< Code First > Connection MySQL database