在 ASP.NET Core 6 Program.cs 中配置 EF
Configuring EF in ASP.NET Core 6 Program.cs
我被要求制作一个 ASP.NET Core 6 webAPI
,我以前没有使用过 Microsoft 的东西,我正在关注 MS 文档 Here 在文档中他们教如何配置 EF
在带有 Main
方法的 Startup.cs
文件中(他们忘记更新文档了?),ASP.NET Core 6
中没有,我的 Program.cs
没有Main
方法,看起来像这样:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
在这种情况下如何将 EF 添加到 Program.cs
?
编辑: 以及如何在没有 class 的情况下添加 CreateDbIfNotExist()
(Docs) 方法?
这是asp.net核心6中的代码:
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<"YOUR_DBCONTEXT">(option =>
option.UseSqlServer(builder.Configuration.GetConnectionString(CONNECTION_STRING)));
用您的数据更改 YOUR_DBCONTEXT 和 CONNECTION_STRING。
我被要求制作一个 ASP.NET Core 6 webAPI
,我以前没有使用过 Microsoft 的东西,我正在关注 MS 文档 Here 在文档中他们教如何配置 EF
在带有 Main
方法的 Startup.cs
文件中(他们忘记更新文档了?),ASP.NET Core 6
中没有,我的 Program.cs
没有Main
方法,看起来像这样:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
在这种情况下如何将 EF 添加到 Program.cs
?
编辑: 以及如何在没有 class 的情况下添加 CreateDbIfNotExist()
(Docs) 方法?
这是asp.net核心6中的代码:
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<"YOUR_DBCONTEXT">(option =>
option.UseSqlServer(builder.Configuration.GetConnectionString(CONNECTION_STRING)));
用您的数据更改 YOUR_DBCONTEXT 和 CONNECTION_STRING。