Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF code-first PluralizingTableNameConvention for ONE DbSet

How do I toggle this convention PluralizingTableNameConvention for only a single table/DbSet? As far as I can tell, I can only do this to all the DbSets for a given DbContext

like image 845
George R Avatar asked Nov 22 '11 06:11

George R


1 Answers

If you have only one entity which is mapped to a table that is not pluralizeed then you can remove the PluralizingTableNameConvention and manually configure the table name of the entity.

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Item>().ToTable("Items");
    }
 }

Or if it is the otherway around

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Item>().ToTable("Item");
    }
 }
like image 94
Eranga Avatar answered Sep 27 '22 20:09

Eranga