TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

Scaffold EF Core models using Handlebars templates.
MIT License
208 stars 53 forks source link

GenerateDbSets net setting set-property-name with TransformPropertyName #223

Open mwmccallum opened 1 year ago

mwmccallum commented 1 year ago

Describe the bug SQL Table name => "switch", which EF Core converts to "_switch".

TableNameChanges.txt has "_switch:switches"

After Scaffold: Context.cs: public virtual DbSet<dbo.switches> _switch { get; set; }

The property name "_switch" is not getting updated to the TableNameChanges.txt setting. The <dbo.switches> is updated property.

Here is example of the PropetyTransformer I have built, which sets the PropertyName for entries in TableNameChanges.txt.

  private EntityPropertyInfo PropertyTransformer ( IEntityType t, EntityPropertyInfo f )
  {
    string tableName = t.GetTableName ();
    // Original Value for Property Type
    string propertyType = f.PropertyType;
    string propertyName = f.PropertyName;
    //Console.WriteLine ( "PropertyTransformer: {0}.{1}: {2}", tableName, f.PropertyName, f.PropertyType );

    #region Property Type Replacements
    // Table Name Replacements
    if ( this.ReplaceEntityNames ( f.PropertyType ) != propertyType )
    {
      propertyType = this.ReplaceEntityNames ( f.PropertyType );
    }

    #endregion

    #region Property Name Replacements   
    if ( this.ReplaceEntityNames ( f.PropertyName ) != propertyName )
    {
      propertyName = this.ReplaceEntityNames ( f.PropertyName );
    }   
    #endregion

    return new EntityPropertyInfo ( propertyType, propertyName, f.PropertyIsNullable );
  }

The expected entry for Context.cs: public virtual DbSet<dbo.switches> switches { get; set; }

All of the other references in Context.cs are properly updated to "switches". It is only the DbSet line property name that is not set correctly.

Based on scanning the project online, it appears the issue is here, where "set-property-name" is set to "entityType.GetDbSetName()" instead of the result of "EntityTypeTransformationService.TransformPropertyName"

        private void GenerateDbSets(IModel model)
        {
            var dbSets = new List<Dictionary<string, object>>();

            foreach (var entityType in model.GetScaffoldEntityTypes(_options.Value))
            {
                if (IsManyToManyJoinEntityType(entityType))
                {
                    continue;
                }

                var transformedEntityTypeName = GetEntityTypeName(
                    entityType, EntityTypeTransformationService.TransformTypeEntityName(entityType.Name));
                dbSets.Add(new Dictionary<string, object>
                {
                    { "set-property-type", transformedEntityTypeName },
                    { "set-property-name", entityType.GetDbSetName() },
                    { "nullable-reference-types", UseNullableReferenceTypes }
                });
            }

            TemplateData.Add("dbsets", dbSets);
        }

I found references in other Issues to this specific item, but although they were referenced as fixed in v6 preview it doesn't appear it was really corrected. It was #171 that I am referring to.

Let me know if you need anything else.

Thanks, Mike