Entity Framework: Programmatically determining the Entity Set name of an Entity
Here is an extension method for the Object Context that allows you to programmatically derive an Entity Set name associated with a particular entity. To put this in context, when adding a new entity object to an Object Context, you need to specify the associated entity set of the entity you are adding.
Here is an extension method for the ObjectContext class that allows you to do this:
public static string GetEntitySetFullName(this ObjectContext, EntityObject entity)
{
// If the EntityKey exists, simply get the Entity Set name from the key
if (entity.EntitKey != null)
{
return entity.EntityKey.EntitySetName;
}
else
{
string entityTypeName = entity.GetType().Name;
var container = context.MetaDataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
string entitySetName = (from meta in container.BaseEntitySets
where meta.ElementType.Name == entityTypeName
select meta.Name).First();
return container.Name + "." entitySetName;
}
}
Best Regards,
Kevin McNeish
.NET MVP 2002-2009
Chief Architect MM .NET Application Framework
INETA Speaker
www.oakleafsd.com