Openiddict-core: How can one custom rename the database tables generated by the OpenIDDict.

Created on 16 Jan 2017  路  4Comments  路  Source: openiddict/openiddict-core

The code below shows how one can apply a naming convention according to a clients needs. But OpenIDDict creates tables such as OpenIddictTokens etc. How can these be renamed to follow the convention?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Token.Models;

namespace Token.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
               : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            const string

              IDENTITY_PREFIX = "CLIENT9010",

              ASP_NET_USERS = "T0001_Security_User",
              ASP_NET_ROLES = "T0002_Security_Role",
              ASP_NET_USER_ROLES = "T0003_Security_User_Role",
              ASP_NET_USER_LOGIN = "T0004_Security_User_Login",
              ASP_NET_USER_CLAIM = "T0005_Security_User_Claim",
              ASP_NET_ROLE_CLAIM = "T0006_Security_Role_Claim",

              ASP_NET_USER_TOKENS = "T0007_AspNetUserTokens"
              ;

            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            #region Change Identity Tables

            #region User
            //aside:    The PK and FK need to be renamed in the DB to avoid conflict.
            //          Also any migration scripts in future also have to be stripped of any reference to Identity Tables
            builder.Entity<ApplicationUser>(entity =>
            {

                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USERS));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USERS)).Property(p => p.Id).HasColumnName("UserId");

                entity.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Claims"));
                entity.HasMany(r => r.Logins).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Logins"));
                entity.HasMany(r => r.Roles).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Roles"));


                entity.HasIndex(e => e.NormalizedEmail).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "EmailIndex"));
                entity.HasIndex(e => e.NormalizedUserName).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "UserNameIndex"));

            }
                );
            #endregion

            #region Role
            builder.Entity<IdentityRole>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_ROLES));
                entity.HasIndex(e => e.NormalizedName).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "RoleNameIndex"));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLES));
                entity.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "Claims"));
                entity.HasMany(r => r.Users).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "Users"));
            }
            );
            #endregion

            #region Role Claims            
            builder.Entity<IdentityRoleClaim<int>>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
            }
            );
            #endregion

            #region User Claims
            builder.Entity<IdentityUserClaim<int>>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
            }
            );
            #endregion

            #region User Login
            builder.Entity<IdentityUserLogin<int>>(entity =>
            {
                entity.HasKey(e => new { e.LoginProvider, e.ProviderKey }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN)); ;
                entity.Property(e => e.LoginProvider).HasMaxLength(450);

                entity.Property(e => e.ProviderKey).HasMaxLength(450);
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN));

            });
            #endregion

            #region User Role
            builder.Entity<IdentityUserRole<int>>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.RoleId }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLES));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLES));
            });
            #endregion

            #region Tokens
            builder.Entity<IdentityUserToken<int>>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKENS));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKENS));
            });
            #endregion

            #endregion

        }
    }
}

question

Most helpful comment

My Bad. Worked it out. I've included the code below for anyone else wishing to rename the tables:


   protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            const string

              IDENTITY_PREFIX = "CLIENT9030",

              ASP_NET_USER = "T0001_Security_User",
              ASP_NET_ROLE = "T0002_Security_Role",
              ASP_NET_USER_ROLE = "T0003_Security_User_Role",
              ASP_NET_USER_LOGIN = "T0004_Security_User_Login",
              ASP_NET_USER_CLAIM = "T0005_Security_User_Claim",
              ASP_NET_ROLE_CLAIM = "T0006_Security_Role_Claim",
              ASP_NET_USER_TOKEN = "T0007_Security_User_Tokens",
              OPENIDDICT_AUTHORIZATION = "T0010_Security_OpenIDDict_Authorization",
              OPENIDDICT_APPLICATION = "T0011_Security_OpenIDDict_Application",
              OPENIDDICT_SCOPE = "T0012_Security_OpenIDDict_Scope",
              OPENIDDICT_TOKEN = "T0013_Security_OpenIDDict_Token"

              ;

            base.OnModelCreating(modelBuilder);
            // Add your customizations after calling base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<ApplicationUser>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER));
            modelBuilder.Entity<IdentityRole>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLE));
            modelBuilder.Entity<IdentityUserClaim<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
            modelBuilder.Entity<IdentityUserRole<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLE));
            modelBuilder.Entity<IdentityUserLogin<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN));
            modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
            modelBuilder.Entity<IdentityUserToken<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKEN));

            modelBuilder.Entity<OpenIddictApplication>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_APPLICATION));
            modelBuilder.Entity<OpenIddictAuthorization>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_AUTHORIZATION));
            modelBuilder.Entity<OpenIddictScope>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_SCOPE));
            modelBuilder.Entity<OpenIddictToken>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_TOKEN));

        }

All 4 comments

How can these be renamed to follow the convention?

Exactly the same way as the Identity entities.

Thanks but something goes horribly wrong. Including the OpenIDDICT somehow creates all the standard Identity tables again with the normal naming convention. You cant manually delete them, as something is referring to them somewhere. I've include the generation script below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Token.Models;
using OpenIddict.Models;

namespace Token.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
               : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            const string

              IDENTITY_PREFIX = "CLIENT9010",

              ASP_NET_USERS = "T0001_Security_User",
              ASP_NET_ROLES = "T0002_Security_Role",
              ASP_NET_USER_ROLES = "T0003_Security_User_Role",
              ASP_NET_USER_LOGIN = "T0004_Security_User_Login",
              ASP_NET_USER_CLAIM = "T0005_Security_User_Claim",
              ASP_NET_ROLE_CLAIM = "T0006_Security_Role_Claim",
              ASP_NET_USER_TOKENS = "T0007_Security_Tokens",

              OPENIDDICT_APPLICATIONS = "T0012_Security_OPENIDDICT_APPLICATIONS",
              OPENIDDICT_AUTHORIZATION = "T0013_Security_OPENIDDICT_AUTHORIZATION",
              OPENIDDICT_APPLICATION = "T0014_Security_OPENIDDICT_APPLICATION",
              OPENIDDICT_SCOPE = "T0015_Security_OPENIDDICT_SCOPE",
              OPENIDDICT_TOKEN = "T0016_Security_OPENIDDICT_TOKEN"

              ;

            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);




            #region Change OpenIDDict Tables

            builder.Entity<OpenIddictApplication>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, OPENIDDICT_APPLICATION));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_APPLICATIONS)).Property(p => p.Id).HasColumnName("Id");


            });

            builder.Entity<OpenIddictAuthorization>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, OPENIDDICT_AUTHORIZATION));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_AUTHORIZATION)).Property(p => p.Id).HasColumnName("Id");

            });

            builder.Entity<OpenIddictScope>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, OPENIDDICT_SCOPE));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_SCOPE)).Property(p => p.Id).HasColumnName("Id");

            });

            builder.Entity<OpenIddictToken>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, OPENIDDICT_TOKEN));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_TOKEN)).Property(p => p.Id).HasColumnName("Id");

                //entity.HasMany(r => r.).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Claims"));

            });

            #endregion

            #region Change Identity Tables

            #region User
            //aside:    The PK and FK need to be renamed in the DB to avoid conflict.
            //          Also any migration scripts in future also have to be stripped of any reference to Identity Tables
            builder.Entity<ApplicationUser>(entity =>
            {

                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USERS));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USERS)).Property(p => p.Id).HasColumnName("UserId");

                entity.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Claims"));
                entity.HasMany(r => r.Logins).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Logins"));
                entity.HasMany(r => r.Roles).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Roles"));


                entity.HasIndex(e => e.NormalizedEmail).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "EmailIndex"));
                entity.HasIndex(e => e.NormalizedUserName).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "UserNameIndex"));

            }
                );
            #endregion

            #region Role
            builder.Entity<IdentityRole>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_ROLES));
                entity.HasIndex(e => e.NormalizedName).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "RoleNameIndex"));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLES));
                entity.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "Claims"));
                entity.HasMany(r => r.Users).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "Users"));
            }
            );
            #endregion

            #region Role Claims            
            builder.Entity<IdentityRoleClaim<int>>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
                entity.HasIndex(x => x.RoleId).HasName(string.Format("{0}_IX_{1}_RoleID", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
            }
            );
            #endregion

            #region User Claims
            builder.Entity<IdentityUserClaim<int>>(entity =>
            {
                entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
            }
            );
            #endregion

            #region User Login
            builder.Entity<IdentityUserLogin<int>>(entity =>
            {
                entity.HasKey(e => new { e.LoginProvider, e.ProviderKey }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN)); ;
                entity.Property(e => e.LoginProvider).HasMaxLength(450);

                entity.Property(e => e.ProviderKey).HasMaxLength(450);
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN));

            });
            #endregion

            #region User Role
            builder.Entity<IdentityUserRole<int>>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.RoleId }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLES));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLES));
            });
            #endregion

            #region Tokens
            builder.Entity<IdentityUserToken<int>>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKENS));
                entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKENS));
            });
            #endregion

            #endregion



        }
    }
}

Including the OpenIDDICT somehow creates all the standard Identity tables again with the normal naming convention.

OpenIddict doesn't reference Identity nor uses the Identity entities (at least, in the >= beta2 bits).

I suspect you're using inconsistent key types: IdentityRole / IdentityRoleClaim<int>/OpenIddictApplication. Try using the same key type everywhere.

My Bad. Worked it out. I've included the code below for anyone else wishing to rename the tables:


   protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            const string

              IDENTITY_PREFIX = "CLIENT9030",

              ASP_NET_USER = "T0001_Security_User",
              ASP_NET_ROLE = "T0002_Security_Role",
              ASP_NET_USER_ROLE = "T0003_Security_User_Role",
              ASP_NET_USER_LOGIN = "T0004_Security_User_Login",
              ASP_NET_USER_CLAIM = "T0005_Security_User_Claim",
              ASP_NET_ROLE_CLAIM = "T0006_Security_Role_Claim",
              ASP_NET_USER_TOKEN = "T0007_Security_User_Tokens",
              OPENIDDICT_AUTHORIZATION = "T0010_Security_OpenIDDict_Authorization",
              OPENIDDICT_APPLICATION = "T0011_Security_OpenIDDict_Application",
              OPENIDDICT_SCOPE = "T0012_Security_OpenIDDict_Scope",
              OPENIDDICT_TOKEN = "T0013_Security_OpenIDDict_Token"

              ;

            base.OnModelCreating(modelBuilder);
            // Add your customizations after calling base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<ApplicationUser>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER));
            modelBuilder.Entity<IdentityRole>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLE));
            modelBuilder.Entity<IdentityUserClaim<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
            modelBuilder.Entity<IdentityUserRole<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLE));
            modelBuilder.Entity<IdentityUserLogin<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN));
            modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
            modelBuilder.Entity<IdentityUserToken<string>>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKEN));

            modelBuilder.Entity<OpenIddictApplication>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_APPLICATION));
            modelBuilder.Entity<OpenIddictAuthorization>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_AUTHORIZATION));
            modelBuilder.Entity<OpenIddictScope>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_SCOPE));
            modelBuilder.Entity<OpenIddictToken>().ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, OPENIDDICT_TOKEN));

        }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonathancod picture jonathancod  路  3Comments

levitatejay picture levitatejay  路  3Comments

prilutskiy picture prilutskiy  路  4Comments

mattwcole picture mattwcole  路  3Comments

AlphaCreativeDev picture AlphaCreativeDev  路  3Comments