Skip to content

[Bug] Transforming property name generates multiple HasColumnName() lines #213

@j-childers

Description

@j-childers

Describe the bug
If the casing of a SQL column name does not match the default property name returned by IEntityType.GetProperties(), scaffolding generates a HasColumnName call for both the original name returned from IEntityType.GetProperties(), as well as the actual database column name.

Expected behavior

Generate a single HasColumnName call with the correct SQL column name.

To Reproduce
Create and scaffold a database with SQL table (note that the identity column ends in "ID", all caps):

CREATE TABLE [dbo].[TestTable](
	[TestTableID] [int] IDENTITY(1,1) NOT NULL,
	[SomeOtherColumn] [nvarchar](10) NULL
) ON [PRIMARY]

In this case, IEntityType.GetProperties() will return a property called TestTableId ('d' is lower-case).

Using the following design time options:

        public void ConfigureDesignTimeServices(IServiceCollection services)
        {
            services.AddHandlebarsScaffolding();

            // Sample property renaming from EntityFrameworkCore.Scaffolding.Handlebars documentation
            services.AddHandlebarsTransformers2(
                // Rename EntityID to Id
                propertyTransformer: (e, p) =>
                    $"{e.Name}Id" == p.PropertyName
                        ? new EntityPropertyInfo(p.PropertyType, "ID", false)
                        : new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable));
        }

Produces:

                entity.Property(e => e.ID) // Correct new property name
                    .HasColumnName("TestTableId")  // Incorrect: Default property name from EFCore.IEntityType.GetProperties
                    .ValueGeneratedOnAdd()
                    .HasColumnName("TestTableID"); // Correct SQL column name

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions