Learn how to establish a connection with asp.net core + Ef + mysql, asp. netcore
Note:
1. Use nuget to import Microsoft. EntityFrameworkCore. Tools and MySql. Data. EntityFrameworkCore.
2. DataContext must declare that a constructor accepts a dbcontextoptions <DataContext> and must use it as the DbContext base constructor.
3. You must add sslmode = none to the connection string.
Operation Code:
Datacontext class
Public class DataContext: DbContext
{
Public DataContext (DbContextOptions <DataContext> options): base (options)
{
}
}
Added ConfigureServices in the startuo class.
Var connection = Configuration. GetConnectionString ("SqlServerConnection ");
Services. AddDbContext <DataContext> (options => options. UseMySQL (connection ));
Add a database connection string to the deletebench.
{
"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 =; port = 3306; database = schooldb; sslmode = none ;"
}
}