如何使用 IdentityDbContext 而不是 DbContext

How to use IdentityDbContext instead of DbContext

我正在学习有关如何构建 MVC 项目的教程。我是初学者,所以我按照教程中的确切步骤进行操作。 到目前为止,我的 ApplicationDbContext 继承自 DbContext 并且工作正常。 现在我希望我的 ApllicationDbContext 继承 IdentityDbContext 而不是 DbContext,但出现以下错误 Errors screenshot。 我安装了所需的 NuGet 包(Microsoft.AspNetCore.Identity.EntityFrameworkCore 和 Microsoft.AspNetCore.Identity.UI)

这是我的ApplicationDbContext.cs

using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.EntityFrameworkCore;
using RockyWebsite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RockyWebsite.Data
{
    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }

        public DbSet <Category> Category { get; set; }
        public DbSet<ApplicationType> ApplicationType { get; set; }
        public DbSet<Product> Product { get; set; }
    }
}

还有我的Startup.cs

using Microsoft.Extensions.Hosting;
using RockyWebsite.Data;

namespace RockyWebsite
{
    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<ApplicationDbContext>(options => options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
            services.AddIdentity<IdentityUser>()
               
               .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddControllersWithViews();
            services.AddHttpContextAccessor();
            services.AddSession(Options=> {

                Options.IdleTimeout = System.TimeSpan.FromMinutes(10);
                Options.Cookie.HttpOnly = true;
                Options.Cookie.IsEssential = true;
            
            });
        }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/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.UseSession();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

如有任何帮助或建议,我们将不胜感激。

谢谢!

您似乎安装了 2 个软件包 entity framework;

using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.EntityFrameworkCore;

既然你用的是.NET CORE,那你就卸载吧; Microsoft.AspNet.Identity.EntityFramework

根据您的上下文替换以下代码

 public class ApplicationDbContext : IdentityDbContext<IdentityUser>{

   //Your DbSets

}

而不是

 public class ApplicationDbContext : IdentityDbContext {

 //Your DbSets   

}

并在您的上下文中编写以下方法:

  protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);                   
    }