我们有一个使用System.Data.Entity.Design.PluralizationServices
的T4模板来处理某些模型生成的表名。
当我们在名称中运行Status
的表格时,例如CompanyStatus
,Singularize
方法会返回CompanyStatu
。我在下面有一些示例代码。
如何才能正确查看CompanyStatus?
void Main()
{
// Sanity checks
Console.WriteLine(Singularize("Cats"));
Console.WriteLine(Singularize("Cat"));
Console.WriteLine(Singularize("Status"));
// The real issue
Console.WriteLine(Singularize("CompanyStatus"));
}
static string Singularize(string word)
{
var singularword = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(System.Globalization.CultureInfo.GetCultureInfo("en-us")).Singularize(word);
return singularword;
}
返回:
猫
猫
状态
CompanyStatu
我也尝试过:
static string Singularize(string word)
{
var pluralizationService = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(System.Globalization.CultureInfo.GetCultureInfo("en-us"));
((ICustomPluralizationMapping)pluralizationService).AddWord("companystatus", "companystatuses");
var singularword = pluralizationService.Singularize(word);
return singularword;
}
答案 0 :(得分:0)
Humanizer会开箱即用吗?
// Note that you have to set the "inputIsKnownToBePlural" flag to false,
// or it will behave the same as PluralizationServices.
Console.WriteLine("Statuses".Singularize(inputIsKnownToBePlural: false));
Console.WriteLine("Status".Singularize(inputIsKnownToBePlural: false));
Console.WriteLine("CompanyStatus".Singularize(inputIsKnownToBePlural: false));
Console.WriteLine("CompanyStatuses".Singularize(inputIsKnownToBePlural: false));