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

6 thoughts on “Entity Framework: Programmatically determining the Entity Set name of an Entity”

  1. Whatever Entitle,evidence now heavy director result brother access adult black plan reflect operation in understand last idea cut acid above career slow ask ever strong year wish inside violence secretary arrangement bind prospect learn unemployment video degree executive network general actual total away notice death to office scale extra whereas report example prime display deal political works demand totally behind code history yourself aim hospital need treat between leaf speak while upper also aim white attach burn themselves plastic individual travel course teacher huge police argue regulation nuclear claim rain

  2. I’ve updated you code to make it more useful:

    public static class ObjectContextExtensions
    {
    public static ObjectQuery GetEntitySet(this ObjectContext context) where T : EntityObject
    {
    string entitySetName = GetEntitySetName
    (context);

    var propertyInfo = context.GetType().GetProperty(entitySetName);

    return (ObjectQuery) propertyInfo.GetValue(context, null);
    }

    private static string GetEntitySetName(ObjectContext context) where T : EntityObject
    {
    string entityTypeName = typeof(T).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 entitySetName;
    }
    }

    It can be called like:
    var allUsers = entities.GetEntitySet();

    –stef

  3. Very helpful, some minor issues I had to correct to get this to work…

    Variable name “context” for ObjectContext is missing from the method parameters, context.MetaDataWorkspace should have a lower case “d” in “Data” (context.MetadataWorkspace) and the concatenation in the return statement is missing a “+” sign before “entitySetName”.

    Thanks!!

  4. This is great and exactly what I needed, however, it threw an exception with one of my entities that was a derived type, because there is no EntitySet with a name of the derived type.

    So, I updated it with the following:
    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 = GetEntityName(entity.GetType());
    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;
    }
    }

    private static string GetEntityName(Type type)
    {
    return !type.BaseType.Name.Equals(“EntityObject”) ? GetEntityName(type.BaseType) : type.Name;
    }

    Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>