3

我有一个 postgres 数据库,其中一个表名为 masterdata。当我现在跑步时

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

我得到以“错误”德语命名的实体模型。

名称从更改masterdataMasterdatum

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

如何关闭此行为?我的系统语言是德语。我的VS语言是英语。

4

1 回答 1

0

就像我在这里已经提到的那样,这个问题与德语无关。要解决它,您必须手动重命名它,因为在不禁用整个复数过程的情况下没有要注入的配置。因此,您要么必须创建一个脚本来自行重命名文件和引用,要么使用 ef-scaffolding 的把手实现:EntityFrameworkCore.Scaffolding.Handlebars

因为它是设计时服务,所以您应该将它放入另一个命名空间。在此服务中,每当解析名称时,您就有机会更改此名称:

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")
        }
    }
}

现在每次执行脚手架时,此服务都会重命名DatumData.

请记住,此代码在所有类型名称中都替换Datum为。Data如果您有一个包含 的域实体Datum,因为如果您不是本地人,有时您不想将非常特定的阶段翻译成英语,这也将被重命名。所以这不是理想的解决方案。

于 2022-03-04T19:48:01.210 回答