Problems adding Identity to ASP.NET Core project using EntityFrameworkCore

asp.net-core asp.net-core-identity entity-framework-core

Question

I'm trying to add Identity to my project so I can add login functionality (I'm following an online course). I've followed the instructions as below but I'm getting the following error when trying to add-migration to update my database with the changes:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Unable to create an object of type 'AppDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I've tried specifically finding Microsoft.AspNetCore.Razor.Runtime in NuGet packages and add it but it's not made any difference - anyone has any ideas?


Instructions followed to get to this point:

  • Go to Dependencies > Manage NuGet Packages
  • Add the following packages:
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore

  • Microsoft.AspNetCore.Identity.UI

  • Change where AppDbContext inherits from in Models.AppDbContext - Change public class AppDbContext : DbContext to
    public class AppDbContext : IdentityDbContext<IdentityUser>
  • Do a new migration and update database to make these changes to the database

EDIT: My Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MLD.Models;

namespace MLD
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940

        public IConfiguration Configuration { get; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<AppDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); // need to pass in the connection string here
            services.AddControllersWithViews(); // brings in support for working with MVC in .Net Core
            services.AddScoped<Repository>();
            services.AddHttpContextAccessor();
            services.AddSession(); // brings in session capability
        }

        // 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(); // redirects HTTP to HTTPS
            app.UseStaticFiles(); // makes sure it serves JS, CSS, images
            app.UseRouting();
            app.UseSession();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=LymphSite}/{action=Index}/{id?}");
            });
        }
    }
}
1
1
2/13/2020 2:24:57 PM

Accepted Answer

Well, for working with Identity you should add Identity to your services:

services.AddDefaultIdentity<IdentityUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

We also need to add the middleware. The location of where we insert the middleware is important. add below code before app.UseEndpoints middleware.

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=LymphSite}/{action=Index}/{id?}");
        });

you can find more explanation about how configuring Identity in this link.

let me know what happened. good luck.

0
2/13/2020 10:29:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow