Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Default Identity Table Names

I would like to rename the default identity table names:

  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers

I understand how to do this using EF6:

 protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>()
            .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
        modelBuilder.Entity<User>()
            .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
    }

However I'm struggling with EF7 as the DbModelBuilder has been replaced with ModelBuilder. Any suggestions?

like image 914
Reafidy Avatar asked Mar 16 '23 14:03

Reafidy


2 Answers

You must use ForSqlServer or ForRelational in the SqlServerPropertyBuilder to change the column name and in the modelBuilder to change the table name

modelBuilder.Entity<IdentityUser>()
                .Property(o => o.Id)
                .ForSqlServer()
                .Column("User_Id")

 modelBuilder.Entity<IdentityUser>()
                    .ForSqlServer()
                    .Table("User")

Update : For the beta 5 is not longer mandatory to use ForSqlServer

like image 155
Pedro Fillastre Avatar answered Mar 24 '23 00:03

Pedro Fillastre


The following code works for ASP.NET MVC Core. You may ask yourself why you would want to change the default names? Well, what if you wanted to host several client sites, with their own authentication, on the same database to avoid additional hosting costs associated with having additional databases. Remember to delete the existing migration scripts and initialise with the changes. Hope this helps:

protected override void OnModelCreating(ModelBuilder builder)
    {
        const string

          IDENTITY_PREFIX = "CLIENT9002",


          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.HasMany(d => d.recipecreatedby).WithOne(p => p.ApplicationUserCreatedBy).HasForeignKey(d => d.createdby); //.IsRequired();
            entity.HasMany(d => d.recipechangedby).WithOne(p => p.ApplicationUserChangedBy).HasForeignKey(d => d.changedby);

            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<ApplicationRole>(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

        #region Recipes
        builder.Entity<recipe>(x =>
        {
            x.HasKey(t => new { t.recipeid });
            x.HasIndex(i => new { i.slugid });
            x.HasIndex(i => new { i.name });
      //      x.HasMany(r => r.Ingredients).WithOne().HasForeignKey(rc => rc.RecipeID).IsRequired();
        });

      //  builder.Entity<Ingredients>().HasKey(t => new { t.IngredientID });

        #endregion

        #endregion

    }
like image 33
Bhail Avatar answered Mar 24 '23 02:03

Bhail