In .NET, classes and properties use PascalCase
names which are translated by Entity Framework into the table and column names in your database. However Postgres requires every name to be quoted if it contains uppercase characters, and the Postgre ecosystem generally prefers lowercase
and snake_case
naming for better compatibility with other tools and frameworks.
Updated Strategy
Changing the naming convention is easy now with the EFCore.NamingConventions package:
dotnet add package EFCore.NamingConventions
public class YourDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(...).UseSnakeCaseNamingConvention();
base.OnConfiguring(optionsBuilder);
}
}
This library supports multiple naming schemes that can be used with any relational driver:
- Snake Case:
some_table_name
- Lower Case:
sometablename
- Camel Case:
someTableName
- Upper Case:
SOMETABLENAME
- Upper Snake Case:
SOME_TABLE_NAME
Older Manual Naming
Andrew Lock has a great article on configuring PostgreSQL naming. It uses the following regex method as a simple converter from pascal-case and camel-case names into snake-case.
public static string ToSnakeCase(this string input)
{
if (string.IsNullOrEmpty(input)) { return input; }
var startUnderscores = Regex.Match(input, @"^_+");
return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLower();
}