1.下载依赖包
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
MySql.EntityFrameworkCore
2.配置appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"stu": "server=localhost;user id=root;database=Student;Pwd=123456;port=3306"
}
}
3.创建数据上下文
public class StuDbContext:DbContext
{
public StuDbContext(DbContextOptions<StuDbContext>options):base(options)
{
}
//映射
public DbSet<Student> Student { get; set; }
}
4.Startup
services.AddDbContext<StuDbContext>(option => option.UseMySQL(Configuration.GetConnectionString("stu")));
services.AddControllersWithViews();
5.迁移
1.Add-Migration ×××××
2.Update-Database