Skip to content

Instantly share code, notes, and snippets.

@vkobel
Created May 27, 2013 08:55
Show Gist options
  • Save vkobel/5655941 to your computer and use it in GitHub Desktop.
Save vkobel/5655941 to your computer and use it in GitHub Desktop.
Simple method to retrieve the keys of an entity in EF. The thing here is to convert the dumb DbContext into a much more powerful ObjectContext.
public IEnumerable<string> GetPrimaryKeyNames<T>() {
ObjectSet<T> objCtx = (ctx as IObjectContextAdapter).ObjectContext.CreateObjectSet<T>();
return objCtx.EntitySet.ElementType.KeyMembers.Select(k => k.Name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment