当我尝试运行我的程序时,遇到了这个异常。我没有默认构造函数,一切似乎都是正确的。
using ExamSchedule.Core.Models;
using Microsoft.EntityFrameworkCore;
namespace ExamSchedule.Core;
public class ScheduleContext : DbContext
{
public ScheduleContext(DbContextOptions<ScheduleContext> opt):base(opt){}
public DbSet<Location> Locations { get; set; }
}
和
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ScheduleContext>(opt =>
opt.UseNpgsql( "Host=localhost:5435;Database=postgres;Username=postgres;Password=postgres"));
app.MapGet("api/parse", async (ScheduleContext ctx) =>
{
return await ctx.Locations.ToListAsync();
});
更新: 调用上面的函数会导致下一个异常:
System.InvalidOperationException:尚未为此 DbContext 配置数据库提供程序。可以通过重写“DbContext.OnConfiguring”方法或在应用程序上使用“AddDbContext”来配置提供程序 服务提供商。如果使用“AddDbContext”,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象,并将其传递给 DbContext 的基本构造函数。 在 Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProviderscopedProvider,DbContextOptions contextOptions,DbContext context) 在 Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() 在 Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() 在 Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastruct.IInfrastruct
.get_Instance() 在 Microsoft.EntityFrameworkCore.Infrastruct.Internal.InfrastructExtensions.GetService(IInfrastruct1 访问器,类型 serviceType) 在 Microsoft.EntityFrameworkCore.Infrastruct.Internal.InfrastructureExtensions.GetService[TService](IInfrastruct1 访问器) 在 Microsoft.EntityFrameworkCore.Infrastruct.AccessorExtensions.GetService[TService](IInfrastruct1 访问器) 在 Microsoft.EntityFrameworkCore.Infrastruct.DatabaseFacade.get_Dependency() 在 Microsoft.EntityFrameworkCore.Infrastruct.DatabaseFacade.get_CurrentTransaction() 在 System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(对象 obj,WriteStack& 状态,Utf8JsonWriter writer) 在 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite(Utf8JsonWriter writer,T 值,JsonSerializerOptions 选项,WriteStack& 状态) 在 System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer、T& 值、JsonSerializerOptions 选项、WriteStack& 状态) 在 System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(对象 obj,WriteStack& 状态,Utf8JsonWriter writer) 在 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite(Utf8JsonWriter writer,T 值,JsonSerializerOptions 选项,WriteStack& 状态) 在 System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer、T& 值、JsonSerializerOptions 选项、WriteStack& 状态) 在 System.Text.Json.Serialization.Converters.ListOfTConverter2.OnWriteResume(Utf8JsonWriter writer、TCollection 值、JsonSerializerOptions 选项、WriteStack& 状态) 在 System.Text.Json.Serialization.JsonCollectionConverter2.OnTryWrite(Utf8JsonWriter writer、TCollection 值、JsonSerializerOptions 选项、WriteStack& 状态) 在 System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer、T& 值、JsonSerializerOptions 选项、WriteStack& 状态) 在 System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer、T& 值、JsonSerializerOptions 选项、WriteStack& 状态) 在 System.Text.Json.Serialization.Metadata.JsonTypeInfo1.SerializeAsync(流 utf8Json,T rootValue,CancellationToken CancellationToken,Object rootValueBoxed) 在 System.Text.Json.Serialization.Metadata.JsonTypeInfo1.SerializeAsync(流 utf8Json,T rootValue,CancellationToken CancellationToken,Object rootValueBoxed) 在 System.Text.Json.Serialization.Metadata.JsonTypeInfo1.SerializeAsync(流 utf8Json,T rootValue,CancellationToken CancellationToken,Object rootValueBoxed) 在 Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.g__WriteAsJsonAsyncSlow|5_0[TValue](HttpResponse 响应,TValue 值,JsonTypeInfo1 jsonTypeInfo) 在 Microsoft.AspNetCore.Http.RequestDelegateFactory.g__ExecuteAwaited|133_0[T](Task1 任务,HttpContext httpContext,JsonTypeInfo1 jsonTypeInfo) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext 上下文)
如果有人在现有数据库中遇到此异常并尝试了所有方法,这里有一个适合您的解决方案。
将其运行到您的项目目录中,以从现有数据库自动创建正确的模型和上下文文件:
dotnet ef dbcontext scaffold "Host=my_host;Database=my_db;Username=my_user;Password=my_pw" Npgsql.EntityFrameworkCore.PostgreSQL