EF代码优先为一个DbSet的PluralizingTableNameConvention

时间:2011-11-22 06:39:39

标签: c# entity-framework-4 entity-framework-4.1

如何仅针对单个表/ DbSet切换此约定PluralizingTableNameConvention?据我所知,对于给定的DbSets

,我只对所有DbContext执行此操作

1 个答案:

答案 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");
    }
 }