今天學習entity架構,直接用類映射資料庫表結構,使用類來操作表。結果提示以下錯誤:
查遍代碼頁沒有找到有cities這個關鍵詞,斷點分析了一下,是entity自動把表名改成了複數形式(entity大哥我真是給你跪了,變複數這樣的屬性也搞成标配)
百度查entity表名複數相關網頁,最終發現entity有個預設變複數的屬性,把這個屬性移除掉就ok了。
對應代碼如下(OnModelCreating就是移除變複數的預設标配
):
using System.Data.Entity;
namespace SportStore.Models.Repository
{
public class EFDbContext: DbContext
{
public DbSet<Cartype> table_cartypes { get; set; }
public DbSet<City> citylist { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
}
}
}
網上還有一種解決方法,我試了沒有效果,可能哪兒有點問題,還是貼出來吧(
[Table(Name = "city")]
就是标明該類對應資料的表名),有誰知道為什麼的請回複告訴我一下,謝謝:
using System;
using System.Data.Linq.Mapping;
namespace SportStore.Models {
[Table(Name = "city")]
public class City {
public Int64 id { get; set; }
public Int64 provinceid { get; set; }
public string city { get; set; }
public string fword { get; set; }
}
}