Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
j-childers opened this issue Jul 29, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@j-childers
Copy link

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
@j-childers j-childers added the bug Something isn't working label Jul 29, 2022
@j-childers
Copy link
Author

In this case, the incorrect name is being added here in HbsCSharpDbContextGenerator.

The correct column name is being generated by GenerateFluentApiCalls later in the same method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant