我正在關注如何構建 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?}");
});
}
}
}
任何幫助或建議將不勝感激。
謝謝!
uj5u.com熱心網友回復:
看來您安裝了 2 個物體框架包;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.EntityFrameworkCore;
由于您使用的是 .NET CORE,因此您應該洗掉并卸載;Microsoft.AspNet.Identity.EntityFramework
uj5u.com熱心網友回復:
在您的背景關系中替換以下代碼
public class ApplicationDbContext : IdentityDbContext<IdentityUser>{
//Your DbSets
}
而不是
public class ApplicationDbContext : IdentityDbContext {
//Your DbSets
}
并在您的背景關系中撰寫以下方法:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429381.html
標籤:C# asp.net-mvc asp.net 身份 数据库上下文
