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