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

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

Open
mwmccallum opened this issue Dec 29, 2022 · 0 comments
Open
Labels
bug Something isn't working

Comments

@mwmccallum
Copy link
Contributor

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

@mwmccallum mwmccallum added the bug Something isn't working label Dec 29, 2022
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