我在 .NET Core 8 中进行首次迁移时出现错误

问题描述 投票:0回答:1

我有一个包含三个项目的解决方案。

  1. EmployeeManagement.Database
    -- 定义
    DbContext
    的类库
  2. EmployeeManagement.Entities
    ——包含我所有实体的类库
  3. EmployeeManagement.WebAPI
    -- Web API 项目

我正在尝试创建一个继承自

DbContext
IdentityDbContext
,但是当我尝试使用我的
dotnet ef migrations add InitialMigration
项目中的
EmployeeManagement.WebAPI
时,出现此错误。

错误:

访问 Microsoft.Extensions.Hosting 服务时发生错误。在没有应用程序服务提供商的情况下继续。错误:无法构造某些服务(验证服务描述符'ServiceType时出错:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:作用域实现类型:Microsoft.AspNetCore.Identity.SecurityStampValidator

1[EmployeeManagement.Entities.ApplicationUser]': Unable to resolve service for type 'System.TimeProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.IdentityBuilderExtensions+PostConfigureSecurityStampValidatorOptions'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[EmployeeManagement.Entities.ApplicationUser] ':尝试激活'Microsoft.AspNetCore.Identity.IdentityBuilderExtensions + PostConfigureSecurityStampValidatorOptions'时无法解析类型'System.TimeProvider'的服务。)(验证服务描述符'ServiceType时出错:Microsoft.Extensions.Options.IPostConfigureOptions
1[Microsoft.AspNetCore.Identity.SecurityStampValidatorOptions] Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Identity.IdentityBuilderExtensions+PostConfigureSecurityStampValidatorOptions': Unable to resolve service for type 'System.TimeProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.IdentityBuilderExtensions+PostConfigureSecurityStampValidatorOptions'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.DataProtectorTokenProvider
1[EmployeeManagement.Entities.ApplicationUser] 生命周期:瞬态实现类型:Microsoft.AspNetCore.Identity.DataProtectorTokenProvider
1[EmployeeManagement.Entities.ApplicationUser]': Unable to resolve service for type 'Microsoft.AspNetCore.DataProtection.IDataProtectionProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.DataProtectorTokenProvider
1[EmployeeManagement.Entities.ApplicationUser]'。)

在程序集“EmployeeManagement.WebAPI”中找不到 DbContext。确保您使用正确的程序集并且类型既不是抽象的也不是泛型的。

我的

DbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
    {
    }

    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
  
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

ApplicationUser
班级:

public class ApplicationUser : IdentityUser
{
}

program.cs

using EmployeeManagement.Database;
using EmployeeManagement.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

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();

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddAuthorization();
builder.Services.AddIdentityCore<ApplicationUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddApiEndpoints();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapIdentityApi<ApplicationUser>();

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();

app.Run();

appsettings.json

{
    "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=EmployeeManagementDb;Persist Security Info=True;User ID=PPPP;Password=PPPPPP;MultipleActiveResultSets=True;"
    },
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*"
}
.net .net-core entity-framework-core entity-framework-6 asp.net-core-identity
1个回答
1
投票

您似乎正在从错误的项目运行迁移。

您可以使用参数定义项目

--startup-project

dotnet ef migrations add InitialMigration --startup-project ../EmployeeManagement.Database

如果您使用 Visual Studio Package Manager Console 来运行该命令,您还应该选择正确的默认项目。

© www.soinside.com 2019 - 2024. All rights reserved.