EntityFramework6.0amp; lt; CodeFirstamp; gt; connect to Mysql database (to) http://blog.csdn.net/kmguo/article/details/19650299
There are a lot of tutorials on using EntityFrame to connect to the Mysql database on the Internet, but many of them are unreliable and reprinted too much. After searching for a long time, I finally configured it. now let's share it.
1. installation:
1. development environment: VS2013 and EF6
2. the Mysql database is Mysql Server 6.0
3. Installation: Mysql for Visual Studio 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
Download location: http://dev.mysql.com/downloads/connector/net/
2. Reference dll:
1. install EF6.0.2 using Nuget;
2. install MySql. Data. Entity. EF6 using Nuget
Note: Nuget must be used for installation. Otherwise, the corresponding dll or configuration information may be missing.
3. configure web. config or app. config
1. replace entitframework node:
2. add a ConnectionString node:
IV. Test C # code
[Csharp]View plaincopy
Using System. Data. Entity;
Namespace StudyEF
{
Public class MyContext: DbContext
{
Public MyContext (): base ("name = MyContext ")
{
}
Public DbSetDatas {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 ());
Var context = new MyContext ();
Context. Datas. Add (new Data {Name = "EF6-MySql "});
Context. SaveChanges ();
}
}
}5. query in Mysql cmd:
6. Database First:
This implementation method is the same as that of the Database under SQL server, but sometimes it cannot connect to Mysql. I tried to use Database to connect to Mysql at the beginning, but I still cannot connect to Mysql server in VS2013. So I used the above Code First. However, after I successfully used Code Frist, I found that my VS2013 can also be connected to the Mysql server. It's amazing. I still don't know the specific reason.
Reference: http://www.nzmk.com/Blogs/BlogsView/tabid/83/EntryId/8/MVC5-EF6-ContosoUniversity-code-first-approach-using-MySQL.aspx