EF Core uses Codefirst to create a new database in MySQL and how the existing MySQL database uses the DB first to generate the domain model

Source: Internet
Author: User

Official Tutorial: https://docs.microsoft.com/en-us/aspnet/core/data/?view=aspnetcore-2.1

Using EF codefirst to create a new database in MySQL, we first use the JSON pair in the Appsettings.json folder to give the MySQL database connection statement, followed by the
Startup.cs uses MySQL intermediate price to inject MySQL service, here, I use the MySQL driver is Pomelo.EntityFramoworkCore.MySql.
Create a new class to make the base class of the data table, and derive a database context class that inherits from DbContext, note! This new database context must have a constructor function.
Once done, create a new database using the following command:
First open the NuGet Management console:

add-Migration Xxxxupdate-database

If we generate a database, we will generate a migration folder. Of course it can be a cmd window, but the commands are different. You can take a look at the official tutorials on your own.
to the.


So how do you use DbContext if you have a database?

Use DB first to create a data table model from an existing MySQL database
The first scenario,
To add from an existing MySQL database to EF Core, use the package console (PM):

" Server=localhost;port=3306;user=root;password=mypass;database=sakila " Mysql.data.entityframeworkcore-o Model

The second scenario (the second option is recommended),

" server=localhost;uid=root;pwd=123456;database=eftests " " Pomelo.EntityFrameworkCore.Mysql " -o Models

Project less good, if there are many tables in the project database, it is recommended to add the existing database in this way

EF Core uses Codefirst to create a new database in MySQL and how the existing MySQL database uses the DB first to generate the domain model

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.