Kevin McNeish Blog

All things iOS, Kindle and .NET

Recent Posts

Tags

News

  • First books in my new book series, "iOS App Development for Non-Programmers" are now available! iBookStore: http://itunes.apple.com/us/book/book-1-diving-in-ios-app-development/id558788074?mt=11 Amazon: http://www.amazon.com/dp/B0097N8XBE Amazon: http://www.amazon.com/dp/B0099RQGMQ

Community

Email Notifications

Archives

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

Comments

Matt Penner said:

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!

# August 18, 2011 3:59 PM
Leave a Comment

(required) 

(required) 

(optional)
 

(required) 

If you can't read this number refresh your screen
Enter the numbers above: