I am following a tutorial on how to build a MVC project. I am a beginner so i am following exact steps as in tutorial. So far, my ApplicationDbContext inherited from DbContext and it was working fine. Now i want that my ApllicationDbContext inherits IdentityDbContext instead of DbContext, but i get the following errors Errors screenshot. I installed required NuGet packages (Microsoft.AspNetCore.Identity.EntityFrameworkCore and Microsoft.AspNetCore.Identity.UI)
This is my 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; }
}
}
And also my 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?}");
});
}
}
}
Any help or suggestion would be much appreciated.
Thanks!
CodePudding user response:
It appears that you installed 2 packages of entity framework;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.EntityFrameworkCore;
Since you are using .NET CORE, you should remove and uninstall; Microsoft.AspNet.Identity.EntityFramework
CodePudding user response:
Replace below code in your context
public class ApplicationDbContext : IdentityDbContext<IdentityUser>{
//Your DbSets
}
istead of
public class ApplicationDbContext : IdentityDbContext {
//Your DbSets
}
and write below method in your context :
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}