1、打开VS2022——扩展——管理扩展——搜索EF Core Power Tools
2、安装EF Core Power Tools扩展(需要重启VS2022)
3、选中项目——右键——EF Core工具——反向工程——添加数据库连接——选则EF Core6
4、选中相应的表或视图——确定
5、选中下面的内容——确定
6、会看到多了一个models文件夹和一个配置文件
7、在Program.cs中添加以下代码(2选1即可)
//向容器中添加依赖注入服务
builder.Services.AddDbContext<DCContext>(options => { options.UseSqlServer("Data Source=localhost;Initial Catalog=DC;Persist Security Info=True;User ID=sa;Password=123456");});
//向容器中添加依赖注入服务
string dbConnection = builder.Configuration.GetConnectionString("DC");
builder.Services.AddDbContext<DCContext>(options => { options.UseSqlServer(dbConnection); });
8、在控制器中使用上下文
//引用上下文
private readonly DCContext _context;
public jwt2Controller(DCContext context)
{
_context = context;
}
//使用EF查询
var aaa = _context.Set<dca_user>();