Precautions:
1. Import Microsoft.EntityFrameworkCore.Tools and MySql.Data.EntityFrameworkCore with NuGet before use
2. DataContext must declare a constructor to accept a Dbcontextoptions < DataContext > must pass it to the DbContext base constructor.
3, the connection string must be added Sslmode=none
Operation Code:
DataContext Class
public class Datacontext:dbcontext
{
Public DataContext (dbcontextoptions<datacontext> options): Base (options)
{
}
}
Configureservices increased in the Startuo class
var connection = configuration.getconnectionstring ("sqlserverconnection");
Services. adddbcontext<datacontext> (options = options. Usemysql (connection));
Add a database connection string in appsettings
{
"Logging": {
"Includescopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"Sqlserverconnection": "Data source=localhost;database=schooldb; User Id=root; Password=123.q;pooling=true; Charset=utf8;port=3306;sslmode=none ",
"Sqlserverconnection": "Server=localhost;userid=root;pwd=;p ort=3306;database=schooldb;sslmode=none;"
}
}
Learn ASP. NET core + Ef+mysql establish a connection