Search code examples
c#asp.net-core-mvcidentityasp.net-core-identity

Problem with identity scaffolding in ASP.NET


I am trying to implement identity auth to my project but it's not creating an applicationdb class like it used to. I am new to coding so I am sorry if it looks dumb

Zenter image description here

I was expecting to get the db class like it used to create before


Solution

  • I am trying to implement identity auth to my project but it's not creating an applicationdb class like it used to. I am new to coding so I am sorry if it looks dumb

    You haven't specified, which version of .NET you are using. However, in latest version or in .NET 8 Microsoft has moved away from a single pre-defined ApplicationDbContext class. Instead, it encourages developers to define their own DbContext while adding the Identity scaffholding.

    You can define your own ApplicationDbContext by yourself before adding Identity or at the same time while adding identity within your project.

    So, while you would click for adding Identity within your project, it would give you the option for DbContext configuration.

    You can do as following:

    enter image description here

    Or you can create manually as following:

    public class ApplicationDbContext : IdentityDbContext<IdentityUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    }
    

    Output:

    After successfull creation, you should have following output:

    enter image description here

    Note: Make sure, that you have the necessary NuGet packages installed for Identity. In ASP.NET Core 8, you typically need Microsoft.AspNetCore.Identity.EntityFrameworkCore package. Please refer to this official document, if you want to study more.