文本转换查找实体(集合) [英] Text Transformations & Finding Entity Plural (Collection)

查看:138
本文介绍了文本转换查找实体(集合)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个.tt模板,我可以使用entity.Name来写出一个实体的名称,例如:

  foreach(ItemCollection.GetItems中的EntityType实体< EntityType>()。OrderBy(e => e.Name))
{
WriteLine(entity.Name);
}

使用正常的转换或T4,如何写出 actual 实体集的名称? (例如Person for Person,它可能是Persons - 但也许我把它改变为设计器中的People,所以我想要这样。)



谢谢! >

Richard

解决方案

我在寻找同一问题的答案这不是太糟糕。



在您的文本模板中,可能在顶部,添加以下行:

p>

 <#@ assembly name =System.Data.Entity.Design#> 
<#@ import namespace =System.Data.Entity.Design.PluralizationServices#>

这样可以创建一个多元化实例:

 <#PluralizationService pluralizer = PluralizationService.CreateService(System.Globalization.CultureInfo.CurrentCulture); #> 

然后要在模板中复数化一个实体,只需使用:

 <#= pluralizer.Pluralize(code.Escape(entity))#> 

当然,您可以替换 code.Escape(entity)



这是你的变量名称。





对于可以在t4模板中导入哪些库,有什么限制吗?
http ://vthornheart.railsplayground.net/blog/archives/655


If I have a .tt template, I can use entity.Name to write out the name of an entity, e.g:

foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{;
    WriteLine(entity.Name); 
}

Using normal transformations or T4, How do I write out the actual name of the entity set? (E.g. for Person, it might be Persons - but maybe I changed it to People in the designer, so I'd want that instead).

Thanks!

Richard

解决方案

I was looking for the answer to the same question and found that it's not too bad. However, instead of getting the EntitySet name, it's quite easy to use the same pluralizer.

In your text template, presumably at the top, add the following lines:

<#@ assembly name="System.Data.Entity.Design" #>
<#@import namespace="System.Data.Entity.Design.PluralizationServices" #>

This allows you to create a pluralizer instance as such:

<# PluralizationService pluralizer = PluralizationService.CreateService(System.Globalization.CultureInfo.CurrentCulture); #>

Then to pluralize an entity in the template, just use this:

<#=pluralizer.Pluralize(code.Escape(entity))#>

Of course, you can replace code.Escape(entity) with the name of your variable storing the entity name.

And that's it!

Sources:
Are there any limitations on what libraries can be imported in a t4 template? http://vthornheart.railsplayground.net/blog/archives/655

这篇关于文本转换查找实体(集合)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆