注意事项:
1、使用前用nuget导入Microsoft.EntityFrameworkCore.Tools和MySql.Data.EntityFrameworkCore
2、DataContext必须声明一个构造函数接受一个dbcontextoptions < DataContext >必须通过它来为DbContext基构造函数。
3、连接字符串一定要加 sslmode=none
操作代码:
datacontext类
public class DataContext:DbContext
{
public DataContext(DbContextOptions<DataContext> options): base(options)
{
}
}
在startuo类里的ConfigureServices增加
var connection = Configuration.GetConnectionString("SqlServerConnection");
services.AddDbContext<DataContext>(options => options.UseMySQL(connection));
在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=;port=3306;database=schooldb;sslmode=none;"
}
}