ASP.NET Core 2.1开发困惑:找不到继承IdentityUser的ApplicationUser.cs类
ApplicationUser.cs in ASP.NET Core 2.1 Hey there! I totally get where you're coming from—following a tutorial only to hit a missing file roadblock is super frustrating. Let's walk through why you might not have the ApplicationUser.cs class and how to get it set up so you can keep building out your user/role management features.
Why You Don't See ApplicationUser.cs
In ASP.NET Core 2.1, the ApplicationUser class is automatically generated only when you create a new project with the "Individual User Accounts" authentication option selected (either for MVC or Razor Pages). If you added identity to an existing project manually, or chose a different authentication template, this file won't be there by default. Upgrading the framework version won't create it retroactively, which is why that didn't help.
Step 1: Manually Create the ApplicationUser Class
No worries—we can make it ourselves. Here's how:
- Right-click your project (or a
Modelsfolder if you have one) → Add → Class. - Name it
ApplicationUser.csand paste this code:using Microsoft.AspNetCore.Identity; namespace YourProjectNamespace.Models // Replace with your actual project namespace { public class ApplicationUser : IdentityUser { // Add any custom user properties here later (like FullName, DateOfBirth) // For now, even an empty class works since it inherits all base IdentityUser fields } }
Step 2: Update Your ApplicationDbContext
Next, you need to tell your database context to use your new ApplicationUser instead of the base IdentityUser. Open your ApplicationDbContext.cs file (usually in the Data folder) and update it like this:
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using YourProjectNamespace.Models; // Make sure to include this namespace namespace YourProjectNamespace.Data { public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } }
Step 3: Configure Identity Services in Startup.cs
Head to your Startup.cs file and find the ConfigureServices method. Update the identity registration to specify ApplicationUser as the user type:
services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders();
Step 4: (Optional) Update Migrations (If Needed)
If you haven't already applied migrations after making these changes, run these commands in the Package Manager Console:
Add-Migration AddApplicationUser Update-Database
Note: If you already have existing user data, this migration won't break anything—ApplicationUser inherits all fields from IdentityUser, so the AspNetUsers table structure stays the same unless you added custom properties.
Once you've done these steps, your project will recognize ApplicationUser just like the tutorial's, and you can proceed to build out your user display and edit interfaces using this model.
内容的提问来源于stack exchange,提问作者esantiago

