TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

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

adding [Question] #166

Closed ghisbo closed 3 years ago

ghisbo commented 3 years ago

I am using EntityFrameworkCore.Scaffolding.Handlebars to generate xaml . it works ok ( i generate it as comment code in entity models)

except for 1 issue: i need the ordinal of the Column to generate the grid.

I modified the HbsCSharpEntityTypeGenerator GenerateProperties method by adding "property-ordinal", but it is not working ( always empty string)

in: HbsCSharpEntityTypeGenerator GenerateProperties

   properties.Add(new Dictionary<string, object>
   {
   { "property-type", propertyType },
   { "property-name", property.Name },
   { "property-ordinal", property.GetColumnOrdinal() },
   { "property-annotations",  PropertyAnnotationsData },
   { "property-comment", _options?.Value?.GenerateComments == true ? GenerateComment(property.GetComment(), 2) : null },
   { "property-isnullable", property.IsNullable },
   { "nullable-reference-types", _options?.Value?.EnableNullableReferenceTypes == true }
  });   

in Class.hbs:

 {{#each properties}}
        <Label Grid.Row="{{property-ordinal}}" Content="{{property-name}}" />
        <TextBox Grid.Row="{{property-ordinal}}" Grid.Column="1" Text="{Binding Path=Entity.{{property-name}} }" />
        {{/each}}
JohnGoldInc commented 3 years ago

Is the .GetColumnOrdinal() not giving the value? May need to have an incrementing variable as you go through properties (should be looped through in ordinal order) if that's the issue?

ghisbo commented 3 years ago

the .getColumnOrdinal does the job ( if i debug with console.writeline) but it seams to dissappear afterwards. I tried it with a counter as well and it gives the same.

JohnGoldInc commented 3 years ago

Maybe use Handlebars built in {{@index}} notation? I'd also try property.GetColumnOrdinal().ToString()

JohnGoldInc commented 3 years ago

Oh did you add property-ordinal to HbsEntityTypeTransformationService as well?

ghisbo commented 3 years ago

That was the problem. I added it there as well Thanks

    public List<Dictionary<string, object>> TransformProperties(List<Dictionary<string, object>> properties)
    {
        var transformedProperties = new List<Dictionary<string, object>>();

        foreach (var property in properties)
        {
            var propTypeInfo = new EntityPropertyInfo(property["property-type"] as string, 
                property["property-name"] as string,
                (property["property-isnullable"] as bool?) == true);
            var transformedProp = PropertyTransformer?.Invoke(propTypeInfo) ?? propTypeInfo;

            transformedProperties.Add(new Dictionary<string, object>
            {
                { "property-type", transformedProp.PropertyType },
                { "property-name", transformedProp.PropertyName },
                { "property-ordinal",property["property-ordinal"] },
                { "property-annotations", property["property-annotations"] },
                { "property-comment", property["property-comment"] },
                { "property-isnullable", transformedProp.PropertyIsNullable },
                { "nullable-reference-types", property["nullable-reference-types"] }
            });
        }