EntityFramework6.0 & lt; CodeFirst & gt; connect to Mysql _ MySQL

Source: Internet
Author: User
Tags mysql for visual studio
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

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.