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
変わりました