Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
11
Help us understand the problem. What is going on with this article?
@kuzira_mood

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

More than 5 years have passed since last update.

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

変わりました

11
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
kuzira_mood
世間の流れについて行けてないです

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
11
Help us understand the problem. What is going on with this article?