.NET EF 6在表名中带有前缀的复数化 [英] .NET EF 6 Pluralization with prefix in table names
问题描述
复数表名称是EF中的默认约定.但是不幸的是,当我添加前缀时,我不能再使用复数形式了.有什么想法吗?
Plural table names are default convention in EF. but when I have added the prefix I can not make names anymore plural unfortunately. Any ideas?
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Types()
.Configure(entity => entity.ToTable("MyPrefix_" + entity.ClrType.Name));
modelBuilder.Conventions.Add<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
推荐答案
PluralizingTableNameConvention 使用了可在任何地方使用的 PluralizationService .因此,您可以继续在配置代码中使用它.
The PluralizingTableNameConvention uses a PluralizationService that can be used anywhere. So you can go ahead and use it in your configuration code.
以下是使用人"模型的示例,该模型应复数为人":
Here is an example using a Model "Person" which should be pluralized to "People":
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
modelBuilder.Types()
.Configure(entity => entity.ToTable("MyPrefix_" + serv.Pluralize(entity.ClrType.Name)));
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
运行此代码后,它将在数据库中正确地将Persons模型复数为"MyPrefix_People".
After running this code it will correctly pluralize the Persons model to "MyPrefix_People" in the database.
要使用PluralizationService,必须引用System.Data.Entity.Design
程序集.
To you use the PluralizationService you will have to reference the System.Data.Entity.Design
assembly.
这篇关于.NET EF 6在表名中带有前缀的复数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!