How to configure Context not to put table name in plural?

4

I'm in an ASP.NET MVC 5 application, setting up a DbContext class.

When EF generates the database, the tables for the application objects are getting the names in the plural. For example:

public System.Data.Entity.DbSet<Teste.Models.Categoria> Categoria { get; set; }      
public System.Data.Entity.DbSet<Teste.Models.Imagem> Imagem { get; set; }

In the database, the tables look like this:

dbo.Categorias.
dbo.Imagems.

How to configure not to be plural?

    
asked by anonymous 15.03.2014 / 22:50

1 answer

8

Add in your implementation of DbContext this method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();   
    base.OnModelCreating(modelBuilder);
}

Another alternative would be to "force" the table name:

Or using annotation Table

[Table("Categoria")]
public class Categoria 
{

}

Or in method OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Categoria>().ToTable("Categoria");
    base.OnModelCreating(modelBuilder);
}
    
15.03.2014 / 23:11