Help us understand the problem. What is going on with this article?

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

More than 3 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

変わりました

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした