将连接字符串写进配置
{ "ConnectionStrings": { "BloggingDatabase": "…" }, }
services.AddDbContext<BloggingContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase" )));
对模型实施反向工程
Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.Tools
Scaffold-DbContext "Server=(localdb)mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
可以通过将 -Tables 参数添加到上述命令来指定要为哪些表生成实体。 例如
-Tables Blog,Post