1、 user 实体类
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public string Pwd { get; set; }
public string Email { get; set; }
public DateTime RegistTime { get; set; }
public DateTime LastLoginTime { get; set; }
public bool Status { get; set; }
}
2、 配置上下文
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
public DbSet<Users> users { get; set; }
}
3、 配置EF CORE
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
}
4、安装EF core 工具
因为我使用的是 core 3.1版本,ef core安装的时候版本看你的core的版本吧
5、命令行使用ef core 生成数据库
dotnet ef migrations add Initial 建立并初始化数据库
dotnet ef database update 更新数据库
dotnet ef migrations add xxxx 更新模型字段后需要执行此命令通知vs重新编译表变动 xxxx为变更的任意字段名 一个就够 系统会自动追加变更添加的其他字段
dotnet ef database update 更新数据库
原文:https://www.cnblogs.com/qzdd/p/12246720.html