1、先导入四个包
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Relational
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
2、在程序包管理控制台执行
Scaffold-DbContext "server=.;Integrated Security=true;database=DBStu" Microsoft.EntityFrameworkCore.SqlServer -OutPutDir Models
Models是你实体类安放的位置
3、在appsettings.json中放入
"ConnectionStrings": {
"DbConnectionString": "Data Source = .;Initial Catalog = DBStu;Integrated Security=true;"
}
Initial Catalog:数据库名称
Data Source:数据源
4、在Program.cs中配置
using Microsoft.EntityFrameworkCore;
builder.Services.AddDbContext<DBStuContext>(option =>
{
string DbConnectionString = builder.Configuration.GetConnectionString("DbConnectionString");
option.UseSqlServer(DbConnectionString);
});文章来源:https://www.toymoban.com/news/detail-461322.html
DBStuContext:是你上下文类的名称文章来源地址https://www.toymoban.com/news/detail-461322.html
到了这里,关于.NET6.0 EF Core连接sql的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!