我已經使用 Entity Framework 創建了我的第一個 (Blazer) 專案。我讓 enable-migrations 和 update-database 作業。現在,我收到以下構建錯誤:
驗證服務描述符“ServiceType:Timesheet.Models.DatabaseContext Lifetime:Singleton
ImplementationType:Timesheet.Models.DatabaseContext”時出錯:嘗試時無法決議型別“Microsoft.EntityFrameworkCore.DbContextOptions`1[Timesheet.Models.DatabaseContext]”的服務激活“Timesheet.Models.DatabaseContext”。
此時拋出錯誤:

我已嘗試多次閱讀該錯誤,但我不確定它指的是什么。我希望有人能指出我正確的方向。這是我的 startup.cs 中的 db 服務
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped(p =>
p.GetRequiredService<IDbContextFactory<DatabaseContext>>().CreateDbContext());
}
我只是不知道下一步該去哪里。任何建議表示贊賞。
編輯 1:添加完整的 Program.cs 檔案
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Timesheet.Data;
using Timesheet.Models;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<DatabaseContext>();
// Add services to the container.
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor()
.AddMicrosoftIdentityConsentHandler();
//builder.Services.AddSingleton<WeatherForecastService>();
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(connectionString));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
uj5u.com熱心網友回復:
var app = builder.Build()在行尾移動您的UseSqlServer行。首先注冊您的所有服務,然后構建應用程式。
uj5u.com熱心網友回復:
如果您看到類似的錯誤
“無法決議型別的服務”
您必須搜索給定的未決議型別。
未決議的型別意味著,您的中間件無法識別給他的型別是什么。
在此示例中,您的服務順序錯誤。您的構建器無法識別什么是“Timesheet.Models.DatabaseContext”。
所以解決方案似乎你必須在 builder.Configuration() 之后呼叫 builder.Build() 操作。
我想當您在其他時間看到相同的錯誤時它會有所幫助。
uj5u.com熱心網友回復:
我找到了錯誤的答案(以及按照上面的建議移動了 builder.Build()。我將 db 服務構建器更改為使用 ContextFactory
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContextFactory<DatabaseContext>(options =>
options.UseSqlServer(connectionString));
并在我的啟動中從更高層洗掉了這個構建器
builder.Services.AddSingleton<DatabaseContext>();
我試圖結合使用 EF6 的腳手架和復制在 EF5 中構建的專案來構建我的創業公司。我想有一些差異我仍然需要研究才能理解。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426634.html
上一篇:EFCoreToArray模擬
