In case it helps anyone, I needed to be able to do this without the knowing the type beforehand (so I couldn't easily do CreateObjectSet<YourEntity>()
because I didn't know YourEntity
), so I was able to adapt @Ladislav 's solution into the following:
// variable "type" is a System.Type passed in as a method parameter
ObjectContext objectContext = ((IObjectContextAdapter)this.context).ObjectContext;
IEnumerable<string> retval = (IEnumerable<string>)objectContext.MetadataWorkspace
.GetType(type.Name, type.Namespace, System.Data.Entity.Core.Metadata.Edm.DataSpace.CSpace)
.MetadataProperties
.Where(mp => mp.Name == "KeyMembers")
.First()
.Value;
Seems kind of odd that MetadataWorkspace.GetType
requires strings of the type name and namespace, instead of a System.Type, but that's the best I could find.