For your first question on how to enumerate the tables in the database, this code will get them for you, of course the ones that has been imported to your EDM which necessarily is not all the tables in your data store.
var tableNames = context.MetadataWorkspace.GetItems(DataSpace.SSpace)
.Select(t => t.Name)
.ToList();
This code will cause an InvalidOperationException with this message:
The space 'SSpace' has no associated collection
And that's because unlike CSpace, SSpace (ssdl) is not loaded until it is needed. and trying to read them with the MetadataWorkspace doesn't count as being needed. It is needed during query compilation, then again at object materialization. So to trick the MetadataWorkspace to load it for us we need to run a query like below just before we run the main query that gives us table names.
string temp = ((ObjectQuery)context.[EntitySetName]).ToTraceString();
You can read more from here: Quick Trick for forcing MetadataWorkspace ItemCollections to load
However, if your intention is to build a dynamic query against your type tables, then you don't need to mess around with SSpace, you have to get it from the CSpace (Conceptual Model). Below is a sample code on how to build a dynamic query with having only a part of table name:
ObjectResult<DbDataRecord> GetAllTypes(string name) {
using (TypeEntities context = new TypeEntities()) {
MetadataWorkspace metadataWorkspace = context.MetadataWorkspace;
EntityContainer container = metadataWorkspace.GetItems<EntityContainer>
(DataSpace.CSpace).First();
string namespaceName = metadataWorkspace.GetItems<EntityType>
(DataSpace.CSpace).First().NamespaceName;
string setName = string.Empty;
string entityName = name + "Type";
EntitySetBase entitySetBase = container.BaseEntitySets
.FirstOrDefault(set => set.ElementType.Name == entityName);
if (entitySetBase != null) {
setName = entitySetBase.Name;
}
EntityType entityType = metadataWorkspace
.GetItem<EntityType>(namespaceName + "." + entityName, DataSpace.CSpace);
StringBuilder stringBuilder = new StringBuilder().Append("SELECT entity ");
stringBuilder
.Append(" FROM " + container.Name.Trim() + "." + setName + " AS entity ");
string eSQL = stringBuilder.ToString();
ObjectQuery<DbDataRecord> query = context.CreateQuery(eSQL);
ObjectResult<DbDataRecord> results = query.Execute(MergeOption.AppendOnly);
return results;
}
}
Code Explanation:
My assumption was that your type table names are ended in "Type" as a postfix (e.g. ColorType), so you can call GetAllType("Color") and it search for ColorType EntityObject in your model and will give you all the possible values. The code might looks scary but it's pretty simple stuff. Basically all it does is that it gets all the required information from the MetaData (like EntitySet name, Namespace name, etc...) based on the method parameter and then build up an EntitySQL query on the fly, then execute it and return the results.