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

AspNet Core Identity tables names are not modified #284

Closed jgilm closed 2 months ago

jgilm commented 2 months ago

Trying to use the lowercase naming convention and AspNet Core Identity, however the AspNet... tables are not affected by the convention. I noticed this when trying to create my own convention, but decided to use this package when I couldn't get my own to work correctly.

Environment:

Usage in DbContext

    override protected void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);

        optionsBuilder.UseLowerCaseNamingConvention();
    }

Looking at the below example, see that the AspNetRoles and AspNetUsers tables are PascalCase, but my Address entity is converted to lowercase. So it looks like this is partially working, but for some reason not with the AspNet... entities. Although, notice that the column names and primary key names are lowercase even for the AspNet... entities

Example migration.

            migrationBuilder.CreateTable(
                name: "address",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    type = table.Column<int>(type: "integer", nullable: false),
                    line1 = table.Column<string>(type: "text", nullable: false),
                    line2 = table.Column<string>(type: "text", nullable: false),
                    city = table.Column<string>(type: "text", nullable: false),
                    state = table.Column<string>(type: "text", nullable: false),
                    postcode = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_address", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "AspNetRoles",           // <========
                schema: "useraccounts",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
                    normalizedname = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
                    concurrencystamp = table.Column<string>(type: "text", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_aspnetroles", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "AspNetUsers",
                schema: "useraccounts",
                columns: table => new
...

Workaround

As a workaround I am adding this convention and iterating over all entities and properties and lowercasing them here. It's working for me in my current project, but not sure if there will be some other issue later.

public class LowercaseNamingConvention : IModelFinalizingConvention
{
    public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
    {
        modelBuilder.Metadata.GetEntityTypes().ToList().ForEach(entityType =>
        {
            entityType.SetTableName(entityType.GetTableName().ToLower());

            entityType.GetProperties().ToList().ForEach(property =>
            {
                property.SetColumnName(property.GetColumnName().ToLower());
            });
        });
    }
}
shearos commented 2 months ago

have you checked out this issue

roji commented 2 months ago

Duplicate of #2