CodexBloom - Programming Q&A Platform

ASP.NET Core 6.0 Identity: Custom ApplicationUser not saving additional properties

👀 Views: 15 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-24
asp.net-core identity entity-framework registration C#

I'm working on a personal project and I'm learning this framework and I'm testing a new approach and This might be a silly question, but I'm working on an ASP.NET Core 6.0 application using Identity for user management... I created a custom `ApplicationUser` class to include additional properties like `DateOfBirth` and `ProfilePictureUrl`. However, when I try to register a new user, the additional properties aren't being saved to the database. Here's a snippet of my `ApplicationUser` class: ```csharp public class ApplicationUser : IdentityUser { public DateTime DateOfBirth { get; set; } public string ProfilePictureUrl { get; set; } } ``` In my `Startup.cs`, I've configured Identity like this: ```csharp services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); ``` For registration, I have a Register method in my `AccountController`: ```csharp [HttpPost] [AllowAnonymous] public async Task<IActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, DateOfBirth = model.DateOfBirth, ProfilePictureUrl = model.ProfilePictureUrl }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { return RedirectToAction("Index", "Home"); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } return View(model); } ``` When I check the database after registration, the `DateOfBirth` and `ProfilePictureUrl` fields are still null. I have also ensured that the corresponding properties exist in my `RegisterViewModel` and are being passed correctly. I tried adding the additional properties to the `DbContext` using the `DbSet<ApplicationUser>` but that didn't seem to help either: ```csharp public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } ``` I've also double-checked that my migrations are up to date, and the database schema includes the new fields. Still, they remain unpopulated. Am I missing something in the configuration or registration process? Any insights would be greatly appreciated! Is there a better approach? The project is a REST API built with C#. Any ideas how to fix this? For reference, this is a production REST API. Thanks for your help in advance! I'm on CentOS using the latest version of C#. Has anyone else encountered this?