views:

80

answers:

1

Given an EntityType, such as "Contact", how can I derive from it the name of the EntitySet it would belong to, i.e. the pluralization such as "Contacts"?

+3  A: 

If you already have an attached entity(obviously you don't need the first line, just use your existing entity):

  Contact c = context.Contracts.Where(x=>x.blah).FirstOrDefault();
  string setName = c.EntityKey.EntitySetName;

Or if you don't:

 string className= typeof( Contact).Name
 var container =   
    context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
    string setName = (from meta in container.BaseEntitySets
                                          where meta.ElementType.Name == className
                                          select meta.Name).First();
Nix
Very nice. I needed this to do additional customization of POCO entities, like adding GetById() methods to all of the entity classes.
Samuel Meacham