Entity Framework 對映的時候預設給資料庫表名新增複數“s”或者“es”的解決辦法

竹林聽雨行發表於2019-03-24

1 #region 實體對映 2 public DbSet UserInfos { get; set; } 3 public DbSet SiteInfos { get; set; } 4 public DbSet ParentBanKuais { get; set; } 5 public DbSet NewInfos { get; set; } 6 public DbSet GgTypes { get; set; } 7 public DbSet AdvertisingInfos { get; set; } 8 public DbSet FriendLinkss { get; set; } 9 public DbSet ChildBanKuai { get; set; } 10 #endregion 複製程式碼

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #region 覆蓋預設的約定----預設情況下EF將實體對映到資料庫中dbo架構下的同名表上 ///

/// 覆蓋預設的約定 /// /// protected override void OnModelCreating(DbModelBuilder modelBuilder) { //移除EF對映預設給表名新增“s“或者“es” modelBuilder.Conventions.Remove(); modelBuilder.Conventions.Remove(); PluralizingTableNameConvention 解決辦法 modelBuilder.Entity().HasKey(u => u.Id); //modelBuilder.Entity().HasKey(u => u.Id).ToTable("UserInfo"); modelBuilder.Entity().HasKey(s=>s.Id); modelBuilder.Entity().HasKey(p=>p.Id); modelBuilder.Entity().HasKey(n => n.Id); modelBuilder.Entity().HasKey(g => g.Id); modelBuilder.Entity().HasKey(g => g.Id).HasRequired(a=>a.GgType); modelBuilder.Entity().HasKey(f => f.Id); //modelBuilder.Entity().HasKey(c => c.Id); modelBuilder.Entity().HasKey(c => c.Id).HasRequired(c => c.ParentBanKuai); modelBuilder.Entity().ToTable("ChildBanKuai"); base.OnModelCreating(modelBuilder); } #endregion   

“/”應用程式中的伺服器錯誤。

物件名'dbo.NewInfo無效。

相關文章