Reprint: EntityFramework 6.0< Code First > Connection MySQL database

Source: Internet
Author: User

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
  1. Using System.Data.Entity;
  2. Namespace Studyef
  3. {
  4. public class Mycontext:dbcontext
  5. {
  6. Public mycontext (): base ("Name=mycontext")
  7. {
  8. }
  9. Public dbset<data> Datas { get;  set;}
  10. }
  11. public class Data
  12. {
  13. public int Id { get;  set;}
  14. public string Name { get;  set;}
  15. }
  16. Class Program
  17. {
  18. static void Main (string[] args)
  19. {
  20. Database.setinitializer (new dropcreatedatabasealways<mycontext> ());
  21. var context = new Mycontext ();
  22. Context.  Datas.add (new data{name="Ef6-mysql"});
  23. Context. SaveChanges ();
  24. }
  25. }
  26. }
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

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.