我被要求制作一個ASP.NET Core 6 webAPI,我以前沒有使用 Microsoft 的經驗,我正在關注 MS Docs Here在他們教如何使用方法EF在Startup.cs檔案中配置的檔案Main(他們忘記更新檔案?),這是中沒有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?
編輯:以及如何在沒有類可駐留的情況下添加CreateDbIfNotExist()(Docs )方法?
uj5u.com熱心網友回復:
這是 asp.net core 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。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/440807.html
標籤:C# 网 asp.net 核心 asp.net-web-api 实体框架 6
上一篇:非泛型型別IQueryable不能與型別引數一起使用
下一篇:無法運行docker容器
