gemon01
gemon01

Reputation: 341

Adding a custom column to AspNetUserRoles table in ASP.NET Core 3.0 - ASP.NET Core IDENTITY

I created the tables through migrations to manage the security of my ASP.NET Core project using ASP.NET Core identity.

ASP.NET Core identity tables

I would need to be able to insert an additional field in the AspNetUserRoles table in order to manage the users already associated with the ADMIN role in a different way.

For example I have ADMINs associated with a USA nation and ADMINs associated with another BRAZIL or ADMIN nation associated with ITALY, but their real role always remains ADMIN.

In practice it is just one more attribute to add when I associate a user with the ADMIN role.

ASP.NET Core identity tables with custom column in AspNetUserRoles table

I tried to extend the IdentityUserRole class in this way:

public class AspNetUserRoles : IdentityUserRole<string>
{
    public string Nation { get; set; }
}

but then when I launch the migration script (code-first) in the migration classes no changes are detected!

PM> Add-Migration aspnetuserrolesupdate -Context ApplicationDbContext

this is the result:

public partial class aspnetuserrolesupdate : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
    }
}

As you can see from the product code no changes were found ....

In my project I have already inserted fields in the AspNetUsers and AspNetRoles tables successfully in this way, but how can I add a third column to the AspNetUserRoles table?

Thank you all

Upvotes: 8

Views: 2611

Answers (1)

gemon01
gemon01

Reputation: 341

I solved it like this:

I modified the injected classes, adding them all to have ApplicationUserRole available

public class ApplicationDbContext: // IdentityDbContext <ApplicationUser, ApplicationRole, string>
     IdentityDbContext <
     ApplicationUser, ApplicationRole, string,
     IdentityUserClaim <string>, ApplicationUserRole, IdentityUserLogin <string>,
     IdentityRoleClaim <string>, IdentityUserToken <string>>
{
     public ApplicationDbContext (DbContextOptions <ApplicationDbContext> options)
         : base (options)
     {
     }
}

Upvotes: 10

Related Questions