Search code examples
entity-framework-coreasp.net-core-identity

How to create my own table using EF in asp net core identity


Currently, I have EF working perfectly fine with asp net core identity. My web app allows users to login in 2 ways: using their local account and using their azure active directory account. However, only the allowed azure directory accounts are allowed to log in. Therefore, I need to create another table to hold those accounts to cross check when they log in.


Solution

  • If you want to create a new table for the Identity project, I suggest you could try to modify the identity EF's dbcontext with the new create model class.

    More details, you could refer to below codes:

    public class UserRelationship
    {
    
        public int UserId { get; set; }
    }
    

    Modify the ApplicationDbContext :

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

    Then you could enable the migration and update the database like this article shows.