entity-framework - database-first ef dbcontext 脚手架将表名翻译为德语
问题描述
我有一个 postgres 数据库,其中一个表名为 masterdata。当我现在跑步时
dotnet ef dbcontext scaffold "Host=xxx;Database=xxx;Username=xxx;Password=xxx" Npgsql.EntityFrameworkCore.PostgreSQL -o Models -f
我得到以“错误”德语命名的实体模型。
名称从更改masterdata
为Masterdatum
modelBuilder.Entity<Masterdatum>(entity =>
{
entity.ToTable("masterdata");
...
如何关闭此行为?我的系统语言是德语。我的VS语言是英语。
解决方案
就像我在这里已经提到的那样,这个问题与德语无关。要解决它,您必须手动重命名它,因为在不禁用整个复数过程的情况下没有要注入的配置。因此,您要么必须创建一个脚本来自行重命名文件和引用,要么使用 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")
}
}
}
现在每次执行脚手架时,此服务都会重命名Datum
为Data
.
请记住,此代码在所有类型名称中都替换Datum
为。Data
如果您有一个包含 的域实体Datum
,因为如果您不是本地人,有时您不想将非常特定的阶段翻译成英语,这也将被重命名。所以这不是理想的解决方案。
推荐阅读
- machine-learning - 我们能否指定在 Vowpal Wabbit 中使用哪种算法(例如,决策树、SVM、集成、NN)?或者,Automl 是否选择算法本身?
- python - 将 pandas 数据帧传递给 FastAPI 用于 NLP ML
- node.js - node.js 应用程序中的 SQL select all 语句返回一个乏味的弃用错误
- regex - 如何替换除最后一位数字以外的所有字符
- mariadb - MariaDB - 将历史数据插入系统版本(时间)表
- java - 单击 ListView 中的项目时,我的应用程序崩溃并创建 SQLite 编译错误。怎么了?
- vue.js - 如何使用 Vue js 在 Button Click 上滚动到页面顶部?
- vue.js - 为什么我的 bootstrap-vue b-table 列标题不在部署中心?
- kubernetes - aws-iam-authenticator 返回已过期的令牌 - eks
- colors - 三个JS如何给用GLTFLoader加载的网格添加颜色和线宽