Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core entity framework (EF Core) table naming convention

.net core entity framework (EF Core) table naming convention plural to single/simple/underscore

Being a fan of single simple underscore naming convention to table names, I feel uncomfortable with the way EF core is naming tables Plural PascalCase.

Model

public class SourceType {
   ... 

DbContext

public class ApplicationDbContext : DbContext {
    public DbSet<SourceType> SourceTypes { get; set; }
    ...

This creates the table with the name SourceTypes (PascalCase and Plural)

I know I can change the generated table name by using [table('source_type')] in the model class.

But, what I need is a method to do it in global manner.

like image 377
Faraj Farook Avatar asked Sep 16 '16 22:09

Faraj Farook


1 Answers

I know the question is old and has been answered, but this NuGet (EFCore.NamingConventions) could be interesting.

This is a NuGet package that handles the naming convention as simple as

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseNpgsql(...)
        .UseSnakeCaseNamingConvention();

It also supports:

  • UseSnakeCaseNamingConvention: FullName becomes full_name
  • UseLowerCaseNamingConvention: FullName becomes fullname
  • UseCamelCaseNamingConvention: FullName becomes fullName
  • UseUpperCaseNamingConvention: FullName becomes FULLNAME
like image 52
Hamid Mayeli Avatar answered Sep 22 '22 06:09

Hamid Mayeli