15

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

6
  • I think you mised one of this usings: using Microsoft.AspNetCore.Builder; or using Microsoft.AspNetCore.Identity;
    – PWND
    Commented Feb 4, 2020 at 11:48
  • I am using namespace Microsoft.AspNetCore.Builder. still, I am getting error Commented Feb 4, 2020 at 11:54
  • please, provide code from your Startup class.
    – PWND
    Commented Feb 4, 2020 at 11:56
  • I have updated the startup.cs code Commented Feb 4, 2020 at 11:59
  • 21
    Have you installed Microsoft.AspNetCore.Identity.EntityFrameworkCore from NuGet? Commented Feb 4, 2020 at 12:02

4 Answers 4

17

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>();

OR

services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDbContext>();
1
  • This worked for me replacing AddDefaultIdentity with AddIdentity: services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDbContext>();
    – Vindberg
    Commented Oct 12, 2022 at 12:23
16

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.

4

Make sure you inherit from IdentityDbContext not DbContext only

1
  • 1
    Can you elaborate on this any? Source code? filename example? This seems like it's a good lead.
    – Joshua K
    Commented May 22, 2023 at 17:55
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.