11

I am using .netcore 3.1. While using the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;    
using Microsoft.Extensions.DependencyInjection;    
using Microsoft.Extensions.Hosting;    
using Microsoft.Extensions.Logging;    
///using AspNetCore3JWT.Data;    
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;        
using Microsoft.AspNetCore.Authentication.JwtBearer;    
using Microsoft.IdentityModel.Tokens;
using Jwt.Data;

namespace Jwt
{
    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.AddControllers()
                   .AddNewtonsoftJson();

            services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                   .AddEntityFrameworkStores<ApplicationDbContext>()

                .AddDefaultTokenProviders();
        }

        // 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();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

I got this error:

error-image-code

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Raju Gaddam
  • 111
  • 1
  • 1
  • 5

4 Answers4

15

I just upgraded a project to .net core 3.1, and had to change the following line from

services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<DBContext>();

To

services.AddDefaultIdentity<ApplicationUser>().AddUserStore<DBContext>();
Dharman
  • 30,962
  • 25
  • 85
  • 135
ShaunOReilly
  • 2,186
  • 22
  • 34
  • This worked for me replacing AddDefaultIdentity with AddIdentity: services.AddIdentity().AddEntityFrameworkStores(); – Vindberg Oct 12 '22 at 12:23
13

In addition to installing the package Microsoft.AspNetCore.Identity.EntityFrameworkCore per serpent5's comment, I also had to install the package Microsoft.AspNetCore.Identity.UI.

cigien
  • 57,834
  • 11
  • 73
  • 112
J.D.
  • 954
  • 6
  • 22
3

Make sure you inherit from IdentityDbContext not DbContext only

Mahmmoud Kinawy
  • 551
  • 4
  • 11
0

check the DbContext if it inherits from IdentityDbContext.

    public class AppUserDbContext: IdentityDbContext<ApplicationUser>
    {}

if it does not, install the package Microsoft.AspNetCore.Identity.EntityFrameworkCore.

D. Azeez
  • 21
  • 3