1.添加nuGet包
2.添加类AppDbcontext
public class AppDbcontext:DbContext
{
public AppDbcontext(DbContextOptions<AppDbcontext> options) :base(options)
{
}
public DbSet<Student> Student { get; set; }
}
3.appsettings.json中添加配置文件
"ConnectionString": {
"SqlServerConnection": "Server=.;Database=StudentDB;Trusted_Connection=True;User ID=sa;Password=123456"
}
4.Startup中ConfigureServices方法中
services.AddDbContextPool<AppDbcontext>(options => options.UseSqlServer(Configuration["ConnectionString:SqlServerConnection"])
);
5.工具->NuGet包管理->程序包管理控制台
add-Migration
update-database
如果給一个表添加一个字段
add-migration AddPhotoPathToStudent
PhotoPath 字段名
Student 表名
update-database
1.删除字段如果没有执行 update-database
Remove-Migration
2.如果执行了update-database
迁移名称
Remove-Migration alterStudentSeedDaa
update-database