EntityFramework 6.0 & lt; Code First & gt; connect to the Mysql database, codefirstmysql

Source: Internet
Author: User
Tags mysql for visual studio mysql login

EntityFramework 6.0 <Code First> connects to the Mysql database, codefirstmysql

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:

<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 a ConnectionString node:

<ConnectionStrings>

<Add name = "MyContext" connectionString = "Data Source = localhost; port = 3306; Initial Catalog = database name; user id = Mysql login username; password = Mysql server password; "providerName =" MySql. data. mySqlClient "/>

</ConnectionStrings>

Iv. Test C # code

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();     }  }}

5. query in Mysql cmd:

Reference: http://www.nzmk.com/Blogs/BlogsView/tabid/83/EntryId/8/MVC5-EF6-ContosoUniversity-code-first-approach-using-MySQL.aspx


Relationship between Entity Framework and code first

CodeFirst is a technical method of EntityFramework, because the traditional programming method is to first establish a database, then model the application based on the database model, and then develop. CodeFirst is literally the code first, first, create the object structure to be mapped to the database in the program, and then the EntityFramework can generate the corresponding database according to the object structure.

How does codefirst connect to the database?

If you are still modifying the model that supports the xxx context after the database is created. Please use Code First to migrate and update the database to find the class library where your database context is located (generally written in the model in the project, and some independent model libraries). Open the Nuget package management console and enter: enable-Migrations Press enter. If yes, Code First migration is enabled for project xxx. Here are several possible errors: 1.No context type was found in the assembly xxx does not find the database context in the current project, that is, the database inherited by DbContext. cs "2.The EntityFramework package is not installed on project xxx the current project has found the data context, but no EntityFrameWork requires installation and input install-package entityframework) if the installation is successful, the Migrations folder appears in the project, which records the changes in each data migration. It is very easy to use, and there is no need to delete the database to regenerate data loss and other issues. Common statement: enable-Migrations-Force replace migration data file update-database update add-migration add new update File
The answer is satisfactory.

Related Article

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.