efcore / EFCore.NamingConventions

Entity Framework Core plugin to apply naming conventions to table and column names (e.g. snake_case)
Apache License 2.0
715 stars 73 forks source link

Naming Conventions for Entity Framework Core Tables and Columns

Nuget

By default, EF Core will map to tables and columns named exactly after your .NET classes and properties. For example, mapping a typical Customer class to PostgreSQL will result in SQL such as the following:

CREATE TABLE "Customers" (
    "Id" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "FullName" text NULL,
    CONSTRAINT "PK_Customers" PRIMARY KEY ("Id")
);

SELECT c."Id", c."FullName"
    FROM "Customers" AS c
    WHERE c."FullName" = 'John Doe';

For PostgreSQL specifically, this forces double-quotes to be added since unquoted identifiers are automatically converted to lower-case - and all those quotes are an eye-sore. But even if we're using another database such as SQL Server, maybe we just hate seeing upper-case letters in our database, and would rather have another naming convention.

Down with same-name identifier tyranny! Simply add a reference to EFCore.NamingConventions and enable a naming convention in your model's OnConfiguring method:

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

This will automatically make all your table and column names have snake_case naming:

CREATE TABLE customers (
    id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    full_name text NULL,
    CONSTRAINT "pk_customers" PRIMARY KEY (id)
);

SELECT c.id, c.full_name
        FROM customers AS c
        WHERE c.full_name = 'John Doe';

Supported naming conventions

Have another naming convention in mind? Open an issue or even submit a PR - it's pretty easy to do!

Important notes