Microsoft Identity Extending tables

lighto22 1 Reputation point
2022-06-03T14:15:48.037+00:00

hello

I am using Microsoft identity in my asp.net core project and i extended the table AspNetUsers by inherit from it a class called appicationUsers, after that i created many tables related to my own project and linked those table with the mentioned one (appicationUsers). when i migrate the DbContext to the database (sql server) two tables have been created (appicationUsers, AspNetUsers) and the relations created between ApplicationUsers and the rest of my tables not between AspNetUsers and them, also when i run the application and started to create users, they were loaded in AspNetUsers table.. so my question is ... how can my tables access to AspNetUsers table if no relation between them??

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,570 questions
Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
345 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 18,786 Reputation points Microsoft Employee
    2022-06-06T01:55:49.597+00:00

    Hi @lighto22 ,

    using Microsoft identity in my asp.net core project ...

    From your description, it seems that when create the project, you selected the Individual Accounts Authentication type, and you will use the Asp.net core Identity to manage users and roles, right?

    208565-image.png

    Generally, when using Asp.net core Identity, to add custom user data to Identity table, we need to inherit the Identity Class, for example extending the AspNetUsers table, the appicationUsers class should like this:

    public class appicationUsers:IdentityUser  
    {  
        public int Age { get; set; }  
    }  
    

    Then,

    public class ApplicationDbContext : IdentityDbContext  
    {  
        public DbSet<appicationUsers> AppicationUsers { get; set; }  
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
            : base(options)  
        {  
        }  
    }  
    

    and change the Identity configuration to use the appicationUsers model

    builder.Services.AddDefaultIdentity<appicationUsers>(options => options.SignIn.RequireConfirmedAccount = true)  
        .AddEntityFrameworkStores<ApplicationDbContext>();  
    

    After migration, we can see the data table as below: the new property is add to the AspNetUsers table, instead of create a appicationUsers table.

    208584-image.png

    when i migrate the DbContext to the database (sql server) two tables have been created (appicationUsers, AspNetUsers) and the relations created between ApplicationUsers and the rest of my tables not between AspNetUsers and them, also when i run the application and started to create users, they were loaded in AspNetUsers table.. so my question is ... how can my tables access to AspNetUsers table if no relation between them??

    From your description, it seems that there is something wrong with your code, try to check your code. If the appicationUsers table and the AspNetUsers table doesn't have relationship, if is hard to query the related data. So, I suggest you could refer the above sample to modify your code, and then you can access the AspNetUsers table via the Dbcontext's AppicationUsers property.

    More detail information about Identity Model customization, see Identity model customization in ASP.NET Core.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    No comments