public static IEnumerable<PropertyInfo> GetKeysForModelType(Type type, DbContext db)
{
var metadataWorkspace = ObjectContextForDb(db).MetadataWorkspace;
var metadata = metadataWorkspace.GetItems<EntityType>(DataSpace.OSpace)
.SingleOrDefault(p => p.FullName == type.FullName);
return metadata.KeyMembers
.Select(k => type.GetProperty(k.Name,
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
.ToList();
}