【问题标题】:While drop database through ef core anExtention Hoisting Error error occured通过 ef core 删除数据库时出现扩展提升错误错误
【发布时间】:2020-07-18 17:35:41
【问题描述】:

我在我的 .Net 核心应用程序中添加数据身份实体框架核心。我在我的应用程序中安装了 Microsoft.AspNetCore.Identity.EntityFrameworkCore 并尝试删除数据库时出现错误

访问 Microsoft.Extensions.Hosting 服务时出错。在没有应用程序服务提供商的情况下继续。错误:无法构造某些服务(验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.SecurityStampValidator1[Domain.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator1[Domain.AppUser]”时出错。 ) (验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Domain.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Domain.AppUser]'。) 无法创建“DataContext”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

我的 DataContext 类是

using System;
using Domain;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Persistence
{
    public class DataContext : IdentityDbContext<AppUser>
    {
          public DataContext(DbContextOptions options) : base(options)
          {
          }

          public DbSet<Value> Values { get; set; }
          public DbSet<Activity> Acitivities {get;set;}

          protected override void OnModelCreating(ModelBuilder builder)
          {

            base.OnModelCreating(builder);
          }
    }
}

我的 Startup.cs 类是

using API.Middleware;
using Application.Activities;
using Domain;
using FluentValidation.AspNetCore;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Persistence;

namespace API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DataContext>(opt => {
                opt.UseSqlite(Configuration.GetConnectionString("DefaultConnection"));
            });

            services.AddControllers();

            services.AddMvc(option => option.EnableEndpointRouting = false)
                    .AddFluentValidation(cfg => cfg.RegisterValidatorsFromAssemblyContaining<Create>());


            var builder = services.AddIdentityCore<AppUser>();
            var identityBuilder = new IdentityBuilder(builder.UserType, builder.Services);
            identityBuilder.AddEntityFrameworkStores<DataContext>();
            identityBuilder.AddSignInManager<SignInManager<AppUser>>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseMiddleware<ErrorHandlingMiddleware>();
            
            if (env.IsDevelopment())
            {
                // app.UseDeveloperExceptionPage();
            }

            // app.UseHttpsRedirection();

            app.UseCors("CorsPolicy");

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            }); 
        }
    }
}

我的 AppUser.cs 类是

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Text;

namespace Domain
{
    public class AppUser : IdentityUser
    {
        public string DisplayName { get; set; }
    }
}

我在这里做错了什么?

【问题讨论】:

  • 我猜你错过了 Identity 的默认令牌提供程序。 var builder = services.AddIdentityCore().AddDefaultTokenProviders();

标签: asp.net entity-framework asp.net-core entity-framework-core migration


【解决方案1】:

我刚刚在var builder = services.AddIdentityCore&lt;AppUser&gt;(); 之前添加了services.TryAddSingleton&lt;ISystemClock, SystemClock&gt;(); 它工作正常

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2014-02-11
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多