Text Transformations & Finding Entity Plural (Collection)

心已入冬 提交于 2019-11-30 03:41:19
Dave

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

Tim

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
}
Mickey Perlstein

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!