Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text Transformations & Finding Entity Plural (Collection)

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

like image 808
Richard Avatar asked Jun 23 '11 04:06

Richard


2 Answers

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

like image 88
Dave Avatar answered Nov 01 '22 07:11

Dave


Once you get your "ItemCollection" from the "CreateEdmItemCollection" method, grab the default Entity Container and you can derive your EntitySet names from that...

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();

foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
    EntitySetBase entitySet = container.BaseEntitySets.FirstOrDefault(set => set.ElementType == entity);

     string pluralizedName = entitySet.Name; //<--- Pluralized Name extracted
}
like image 5
Tim Avatar answered Nov 01 '22 09:11

Tim