I am created a new .Net core web application using vs 2022 and .NET 8.0 Framework. Project created successfully but when i try to add Asp.net core identity using scaffolding identity. I add dbcontext name and 'MyApplicationUser' in User Class section ,then click on Add button. DBcontext class file is added successfully in Area/Identity/Data folder but a class with no name(User Class) is added in this folder.I think the problem is in code-Generator.
Thanks
Although I haven't found a direct solution, I figured out a workaround that achieves the same result.
During project creation when asked for Authentication Type
change it from None
to Individual Accounts
Make a file for the ApplicationUser
file, and make it inherit from IdentityUser
(This file can be put anywhere within the project, I put it in the Data
Folder with the ApplicationDbContext
)
using Microsoft.AspNetCore.Identity;
namespace ScaffoldFixExample.Data
{
public class ApplicationUser : IdentityUser
{
}
}
In the ApplicationDbContext.cs
file, change the following line from
public class ApplicationDbContext : IdentityDbContext
to
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
In the Program.cs
file, change the following line from
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
to
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
In the _LoginPartial.cshtml
in /Views/Shared
, change the beginning from
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
to
@using Microsoft.AspNetCore.Identity
@using ScaffoldFixExample.Data
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
Note: Replace ScaffoldFixExample
with your projects/solutions name you set during creation (root folder name)
Add
- New Scaffold Item
- Identity
Override all files
)DbContextClass
to ApplicationDbContext
Add
Package Manager Console
- View
-> Other Windows
-> Package Manager Console
update-database
Now the custom ApplicationUser
is all setup, and can be used as you normally would.
Note: This does result in a slightly different folder structure than the method that is commonly used/used in the question.
To make it look/be structured the same do the following,
Move the Migrations
folder into the root folder
Move the Data
folder into the /Area/Identity/
folder.
Update the using
in _LoginPartial.cshtml
from @using <Project>.Data
to @using <Project>.Areas.Identity.Data
Add in the @using <Project>.Areas.Identity.Data
in the file /Areas/Identity/Pages/Account/Manage/_ManageNav.cshtml