democontext:
public DemoContext(DbContextOptions<DemoContext> options):base(options) { }
nuget包:
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
Startup
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddDbContext<DemoContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("LocalDb")); }); }
appsettings.json添加连接字符串:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"LocalDb": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Demo;"
},
"AllowedHosts": "*"
}