.NET6.0 EF Core连接sql

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(option =>

{

string DbConnectionString = builder.Configuration.GetConnectionString("DbConnectionString");

option.UseSqlServer(DbConnectionString);

});

DBStuContext:是你上下文类的名称

你可能感兴趣的:(sql,.net,数据库)