LoginSignup
9
12

More than 5 years have passed since last update.

【ASP.NET Identity】デフォルトで、自動生成されるテーブルの名前を変える方法

Posted at

CodeZineさんのASP.NET Identity入門の記事を読みながら、
ASP MVC5でWebアプリケーションを作成した時に迷ったところがあったのでメモ

自動生成されるテーブルの名前を変えたい

プログラムを実行すると以下のテーブルが作られます。

  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers

好きな名前を付けたい・・・

こうすると出来た

Model/IdentiyModel内

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
//(省略)
   protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);

//こんな感じで好きな名前を定義します
            modelBuilder.Entity<IdentityUser>().ToTable("hoge_Users").Property(p => p.Id).HasColumnName("UserId");
            modelBuilder.Entity<ApplicationUser>().ToTable("hoge_Users").Property(p => p.Id).HasColumnName("UserId");
            modelBuilder.Entity<IdentityUserRole>().ToTable("hoge_UserRoles");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("hoge_UserLogins");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("hoge_UserClaims");
            modelBuilder.Entity<IdentityRole>().ToTable("hoge_MyRoles");
        }
}

パッケージマネージャーコンソールからマイグレーションします

Enable-Migrations –ContextTypeName MvcOther.Models.ApplicationDbContext
Add-Migration migrationtoday
Update-Databese

変わりました

9
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
12