C
C#•3mo ago
Gipper

How can I access the custom extra columns I tacked on to the AspNetUsers Identity db table?

I have an ASP.NET MVC app with a Users Model, but I am also using ASP.NET Identity. In order to avoid the confusion of the Identity built-in AspNetUsers table overlapping with my own Users table I simply inherited from Identity in my users model:
public partial class User : IdentityUser
{
public double userHeight {get;set;} // how tall user is
// other extra fields
}
public partial class User : IdentityUser
{
public double userHeight {get;set;} // how tall user is
// other extra fields
}
Which made EFCore simply append the "userHeight" column and other custom columns from the model on to the AspNetUsers table's already existing columns, which I was happy with. Problem being now I don't know how to access those extra columns. Here is how I access the current user entry in the AspNetUsers table, which I believe is how you're supposed to do it:
var currentUserTask = _userManager.GetUserAsync(User);
currentUserTask.Wait();
var currentUser = currentUserTask.Result;
var currentUserTask = _userManager.GetUserAsync(User);
currentUserTask.Wait();
var currentUser = currentUserTask.Result;
However the resulting currentUser variable will only hold the default Identity table columns and none of my extra ones I tacked on. I have tried to do it like so:
var utilizador = _context.Users.Find(currentUser.Id);
var utilizador = _context.Users.Find(currentUser.Id);
But that gives me an error "The instance of entity type 'User' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked." or something else about how the DbContext was already being accessed by another thread which never let go of the context (can't quite remember, something similar to that).
12 Replies
Angius
Angius•3mo ago
.Wait() .Result, camelCase properties 💀 Did you tell EF and Identity to start using your User instead of the default IdentityUser?
Gipper
Gipper•3mo ago
I didn't even know one could do this...Could you please tell me how? I'm still learning...
Angius
Angius•3mo ago
First, your DbContext should inherit from IdentityDbContext<User> Chances are this will be enough
Gipper
Gipper•3mo ago
should I not do it with Wait() and .Result? I don't really have a lot of experience on how to work with tasks, I wasn't using them originally...and my properties aren't in camelcase that is just an example I quickly cooked up for this question 😆
Angius
Angius•3mo ago
async and await is what you use to deal with asynchronous code .Result, .Wait(), all of that should ideally be blanket-banned from the project with an analyzer The one and only proper way to call an asynchrnous method is to await it
- var currentUserTask = _userManager.GetUserAsync(User);
- currentUserTask.Wait();
- var currentUser = currentUserTask.Result;
+ var currentUser = await _userManager.GetUserAsync(User);
- var currentUserTask = _userManager.GetUserAsync(User);
- currentUserTask.Wait();
- var currentUser = currentUserTask.Result;
+ var currentUser = await _userManager.GetUserAsync(User);
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Salman
Salman•3mo ago
haha make sure your DBContext inherits from IdentityDbContext<YourUserModel> and then apply migration . Maybe you haven't applied the migration yet
Gipper
Gipper•3mo ago
After how can I access my extra columns?
Salman
Salman•3mo ago
once you've done that steps, you'd get the new columns in the response
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Salman
Salman•3mo ago
Identity model customization in ASP.NET Core
This article describes how to customize the underlying Entity Framework Core data model for ASP.NET Core Identity.
Salman
Salman•3mo ago
note: you'd also need to update the identity service registration code in Program.cs as shown in the above shared page:
c#
services.AddDefaultIdentity<YourUserModel>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
c#
services.AddDefaultIdentity<YourUserModel>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
Want results from more Discord servers?
Add your server