使用 EntityFrameworkCore 将身份添加到 ASP.NET 核心项目时出现问题

Problems adding Identity to ASP.NET Core project using EntityFrameworkCore

我正在尝试将 Identity 添加到我的项目中,以便我可以添加登录功能(我正在学习在线课程)。我已按照以下说明进行操作,但在尝试添加迁移以使用更改更新我的数据库时出现以下错误:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Unable to create an object of type 'AppDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

我试过专门在 NuGet 包中找到 Microsoft.AspNetCore.Razor.Runtime 并添加它,但没有任何区别 - 有人有任何想法吗?


达到这一点所遵循的说明:

编辑: 我的 Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MLD.Models;

namespace MLD
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940

        public IConfiguration Configuration { get; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<AppDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); // need to pass in the connection string here
            services.AddControllersWithViews(); // brings in support for working with MVC in .Net Core
            services.AddScoped<Repository>();
            services.AddHttpContextAccessor();
            services.AddSession(); // brings in session capability
        }

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

            app.UseHttpsRedirection(); // redirects HTTP to HTTPS
            app.UseStaticFiles(); // makes sure it serves JS, CSS, images
            app.UseRouting();
            app.UseSession();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=LymphSite}/{action=Index}/{id?}");
            });
        }
    }
}

嗯,要使用 Identity,您应该将 Identity 添加到您的 services:

services.AddDefaultIdentity<IdentityUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

我们还需要添加middleware。我们插入中间件的位置很重要。添加下面的代码 before app.UseEndpoints middleware.

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=LymphSite}/{action=Index}/{id?}");
        });

您可以在 link.

中找到有关如何配置 Identity 的更多说明

让我知道发生了什么事。祝你好运。