如何仅针对单个表/ DbSet切换此约定PluralizingTableNameConvention
?据我所知,对于给定的DbSets
DbContext
执行此操作
答案 0 :(得分:26)
如果您只有一个实体映射到未复数的表,则可以删除PluralizingTableNameConvention
并手动配置实体的表名。
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Item>().ToTable("Items");
}
}
或者如果它是相反的
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Item>().ToTable("Item");
}
}