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:
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:
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:
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
.Wait()
.Result
, camelCase
properties 💀
Did you tell EF and Identity to start using your User
instead of the default IdentityUser
?I didn't even know one could do this...Could you please tell me how? I'm still learning...
First, your
DbContext
should inherit from IdentityDbContext<User>
Chances are this will be enoughshould 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 😆
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
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
haha
make sure your
DBContext
inherits from IdentityDbContext<YourUserModel>
and then apply migration . Maybe you haven't applied the migration yetAfter how can I access my extra columns?
once you've done that steps, you'd get the new columns in the response
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
@Gipper please check this , this is what you need to read:
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-8.0#customize-the-model
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.
note: you'd also need to update the identity service registration code in
Program.cs
as shown in the above shared page: