13

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

Eranga
  • 32,181
  • 5
  • 97
  • 96
Richard
  • 5,810
  • 6
  • 29
  • 36

3 Answers3

15

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

Community
  • 1
  • 1
Dave
  • 166
  • 1
  • 2
  • 1
    what if I have set a different name for the EntitySet in the edmx. fail! – Mickey Perlstein Jul 26 '12 at 09:53
  • 2
    I haven't investigated whether this might be an issue (it sounds logical and therefore very probably is) but I don't think it's at all constructive to drop a fail into your answer like that (well, it's rude in my opinion but others might disagree). In the SO spirit, why not come up with a solution instead? That way we all learn something interesting? – Richard Jul 26 '12 at 22:32
  • I had to get rid of that code.escape(entity) stuff so i could use ViewDataTypeShortName for my specific needs, but other than that, it worked like a charm, History => Histories – Dylan Hayes Mar 10 '16 at 21:40
  • Worked great for me by exchanging code.escape with my own value. – Jason W Mar 22 '17 at 05:32
5

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
}
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
Tim
  • 121
  • 1
  • 2
2

you might find these useful:

   string GetEntitySetName(string entityName, EntityContainer container)
{

var list = container.BaseEntitySets.OfType<EntitySet>();
var l = list.Where( f=> f.ElementType.Name == entityName);
string setname = l.Select(c=>c.Name).FirstOrDefault();
return setname ?? string.Empty;
}

string GetEntitySetName(System.Data.Metadata.Edm.EdmType entity, EntityContainer container)
{
    string name = GetAllBaseClasses(entity).Last();
    var out_ = GetEntitySetName(name, container);
    return out_;
}
string[] GetAllBaseClasses(System.Data.Metadata.Edm.EdmType entity, int From = 0)
{
    List<string> types = new List<string>();
    types.Add(entity.Name);

    while (entity.BaseType != null)
    {

        types.Add(entity.BaseType.Name);

        entity = entity.BaseType;

    }

    return types.Skip(From).ToArray();
}
navid
  • 566
  • 6
  • 15
Mickey Perlstein
  • 2,508
  • 2
  • 30
  • 37