database-first ef dbcontext scaffold translates Tablename to German

Viewed 693

I have a postgres database where one table is named masterdata. When I now run

dotnet ef dbcontext scaffold "Host=xxx;Database=xxx;Username=xxx;Password=xxx" Npgsql.EntityFrameworkCore.PostgreSQL -o Models -f

I get Entity Models which are named in "wrong" German.

The name is changed from masterdata to Masterdatum

modelBuilder.Entity<Masterdatum>(entity =>
            {
                entity.ToTable("masterdata");
...

How can I switch off this behaviour? My System language is German. My VS language is English.

1 Answers

Like I already mentioned here, this problem has nothing to do with the German language. To solve it you have to rename it manually because there is no configuration to inject without disabling the whole pluralization process. So you either have to create a script to rename the files and references on your own or you use the handlebars implementation of ef-scaffolding: EntityFrameworkCore.Scaffolding.Handlebars

Because it's a design-time service, you should put it into another namespace. In this service, you get the chance whenever a name is resolved to change this name:

namespace YourDatabaseProject.DesignTime
{
    public class ScaffoldingDesignTimeServices : IDesignTimeServices
    {
        public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
        {
            serviceCollection.AddHandlebarsScaffolding(options =>
            {
                options.LanguageOptions = LanguageOptions.CSharp;
                options.EnableNullableReferenceTypes = true;
                options.EnableSchemaFolders = false;
            });

            serviceCollection.AddHandlebarsTransformers(
                entityNameTransformer: this.ReplaceUnwantedStrings,
                entityFileNameTransformer: this.ReplaceUnwantedStrings,
                propertyTransformer: propertyInfo => new EntityPropertyInfo(this.ReplaceUnwantedStrings(propertyInfo.PropertyType), propertyInfo.PropertyName, propertyInfo.PropertyIsNullable),
                navPropertyTransformer: propertyInfo => new EntityPropertyInfo(this.ReplaceUnwantedStrings(propertyInfo.PropertyType), propertyInfo.PropertyName, propertyInfo.PropertyIsNullable),
                constructorTransformer: propertyInfo => new EntityPropertyInfo(this.ReplaceUnwantedStrings(propertyInfo.PropertyType), propertyInfo.PropertyName, propertyInfo.PropertyIsNullable)
            );
        }

        private string? ReplaceUnwantedStrings(string? s)
        {
            return s?.Replace("Datum", "Data").Replace("datum", "data")
        }
    }
}

Now every time scaffold is executed this service renames Datum to Data.

Keep in mind that this code replaces Datum with Data in all type names. If you have a domain entity that contains Datum, because sometimes you don't want to translate very specific phases to English if you are not native, this will be renamed too. So it's not the ideal solution.

Related