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

Asked

Viewed 4,398 times

4

I am in an ASP.net MVC 5 application, configuring a class DbContext.

When the EF generates the database, the tables referring to the application objects are getting 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 bank, the tables look like this:

dbo.Categorias.
dbo.Imagems.

How to configure not to plural?

1 answer

8


Add to your implementation of DbContext that 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 the Annotation Table

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

}

Or in the method OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Categoria>().ToTable("Categoria");
    base.OnModelCreating(modelBuilder);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.