LoginSignup
0
2

More than 5 years have passed since last update.

EntityFramework のテーブル名を複数形化せずモデルクラス名をそのまま使う

Posted at

1個ずつ指定するならTable属性でモデルクラスに指定

BUSHO.cs
    [Table(nameof(BUSHO))]
    public class BUSHO
    {
      .....
    }

ただ、いちいちTable属性を指定するのは面倒なので、名前の変更を行っているPluralizingTableNameConventionを無効化する。
DbContextOnModelCreatingの中で行えばよい。

AppDbContext.cs
    public class AppDbContext : DbContext
    {
        public AppDbContext()
            : base("name=AppDbContext")
        {
        }
        // Table
        public virtual DbSet<BUSHO> BUSHOs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //テーブル名を複数形化せずモデルクラス名をそのまま使う
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
        }

    }
0
2
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
0
2