Mvc+mysql+ef

Source: Internet
Author: User

There are a lot of cases on the web that EF installs with the NuGet Manager online, so let's talk about the offline situation!

I. Create a folder, such as D:/packages

Add the packages required to install EF and MySQL: entityframework.6.1.3.nupkg

Entityframework.extended.6.1.0.133.nupkg

Entityframework.zh-hans.6.1.3.nupkg

Mysql.data.6.9.8.nupkg

Mysql.data.entity.6.9.8.nupkg

Mysql.web.6.9.8.nupkg

(Put your own build folder according to your own version)

Two. Project right----manage nuget packages---Set---name (for example, package source)---Source (find the folder of your own ef+mysql packages, such as previous d:/packages)---settings ok

Three. VS menu item---Tool---Library Package Manager---Package Management console: (Note: If you have more than one project, in the package management console-The default project, select the project you want to install)

Enter after pm>

Install-package entityframework-version 6.1.3 (add according to your version number) Enter to confirm the installation information

Install-package Entityframwork.zh-hans-version 6.1.3

Install-package mysql.data-version 6.9.8

Install-package mysql.data.entity-version 6.9.8

Install-package mysql.web-version 6.9.8 Installation Complete

Four. Define a context class:

Make it inherit DbContext

For example:

[Dbconfigurationtype (typeof (MySql.Data.Entity.MySqlEFConfiguration))]//Not added in the case will be in the Add-migration Initialcreate error: No Mygrationsqlgenerator found for provider "MySql.Data.MySqlClient". Use Setsqlgenerator in the target migration configuration class method to register additional SQL generators

public class Datacontext:dbcontext

{

Name new connection string name and context class name are different, specify the name of the connection string in the context constructor

Public DataContext (): Base ("Name=" Test ") {

Database.setinitializer<datacontext> (NULL)//non-initialized

}

You add the model, put it in the database

Public dbset<user> User{get;set}

}

Five. Use of Efcodefirst data migration, in the case of changes in the database structure prior to the existence of test data, add to the database operation

Enable-migrations-enableautomaticmigrations: Using Data migration under EF Codefirst

Add-migration Initialcreate Creating an initialization migration

Updata-database-verbose Modify the database to place the added table in the database

To add a table:

Add-migration ADD Table name for example: Add-migration addcompany

Update-database-verbose

Add Field

add-migration field name for example: add-migration name

Update-database-verbose

Delete a field

Add-migration Modify Table name for example: Add-migration modifycompany

Update-database-verbose

Mvc+mysql+ef

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.