TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

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

[Question] Changing names of navigation properties #163

Closed skuami closed 3 years ago

skuami commented 3 years ago

In a project I have a translation table and many other tables with one or more columns to be translated. Those tables are joined through a TranslationKey. The TranslationKey has on each table (not the translation table) a unique index. On the translation table is the primary key constructed with the combination of TranslationKey and LanguageCode.

Since the tables are not joined through the primary key - EF will generate "funny" names for the navigation properties. I posted this question also on Stackoverflow.

I tried to use the navPropertyTransformer. There are to little information in the EntityPropertyInfo to generate a new name for tables with multiple translations. Apart from that I am having also following issue (not sure if it is a bug or if I am doing something wrong):

The nav property gets renamed correctly but the initialization in the constructor gets not renamed: image

Here's the transformation code I am using:

services.AddHandlebarsTransformers(navPropertyTransformer: TransformTranslationKeyNavProperty);

private EntityPropertyInfo TransformTranslationKeyNavProperty(EntityPropertyInfo propertyInfo)
{
        var propName = propertyInfo.PropertyName;
        if (propName.Contains("TranslationKey") && propName != "TranslationKey")
                return new EntityPropertyInfo(propertyInfo.PropertyType, "Translation" + propertyInfo.PropertyType, propertyInfo.PropertyIsNullable);

                return propertyInfo;
}
tonysneed commented 3 years ago

All the transformations need to be done consistently, so that the type names match. For example, see how "Foo" is appended in multiple transformers.

services.AddHandlebarsTransformers(
    entityNameTransformer: n => n + "Foo",
    entityFileNameTransformer: n => n + "Foo",
    constructorTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"),
    propertyTransformer: e => new EntityPropertyInfo(e.PropertyType, e.PropertyName + "Foo"),
    navPropertyTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"));
tonysneed commented 3 years ago

@skuami Did you find my answer helpful? Do you require further assistance with this issue? If not, I will close.

skuami commented 3 years ago

@tonysneed Thanks for your answer. I couldn't solve the problem yet. I am dependend on the open PR with additional information for navPropertyTransform. I will close this question as soon as I could solve it, ok?

skuami commented 3 years ago

@tonysneed As I mentioned on the open PR - the additional NavigationDetail information helping me to solve my problem. I hope the PR can be merged anytime soon. I am using code from the PR for now.