Search code examples
c#databasesqliteasp.net-identityasp.net-core-identity

How to add tables in asp.net core Identity


I created asp.net core project with Authentication -> individual user accounts. I created all the roles and authorised their pages. It is working but i had to uprage the project, adding two more tables. The first table is connected with the second. And the second must be connected with the scaffolded user table. I tried making the relations with OnModelcreating in DbContext but it doesn’t work


Solution

  • According to your description, if you want to modify the identity table, I suggest you could refer to below steps:

    1.Add the new model: e.g:

    public class NewTestModel
    {
        public int Id { get; set; }
        // This is used to make one to one relationship to identity user
        public IdentityUser user { get; set; }
    }
    

    2.Modify the dbcontext:

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

    3.Open the package management console and run below codes:

    Add-Migration Modify1
    
    
    update-database
    

    enter image description here

    Result:

    enter image description here