Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3240606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:06:17+00:00 2026-05-17T18:06:17+00:00

That’s it. It’s pretty simple. I’ve got an edmx and want to be able

  • 0

That’s it. It’s pretty simple. I’ve got an edmx and want to be able to dynamically query it for tables and (hopefully), dynamically build against that table. Is that possible?

=========

UPDATE:

I’ve included all the DB tables, but no views or SP’s, in the context. We have lots of tables that type info (with id’s). So, for example, colors or file type or protocol type. I want to be able to take a type (file) query for tables that might hold the type info (File, FileType) and return it with id.

So, I may look for… Business Unit (or Color, or File) and the code would go off and search the context for BusinessUnit (or Color or File) and BusinessUnitType (or ColorType or FileType). If it finds either one, it will query it and will return all the rows so I can see if this holds type information (I’ll refine it later to only return ID and Description, Abbreviation or Name fields as well as limiting rows etc) and be able to find the associated ID for a particular whatever.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:06:17+00:00Added an answer on May 17, 2026 at 6:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's a simple task in ASP.NET WebForms:If you want to show/hide a control all
That's a mouthful of a question but basically I want to know if I
That's pretty much my question: can VS 2010 check and update header files in
That is how is the maximum colspan, for example: <table> <tr> <td> 1 </td>
That is, without defining a specific 'width' amount for each individual item. As in,
That title is quite a mouthful. Let me try to be as clear as
That is a common question, but I would like to hear some expert opinions.
That might be silly question but I really need to know the answer. If
That's it. The question is in the title I was looking for a cleaner
That is, why does unsigned short var= L'ÿ' work, but unsigned short var[]= Lÿ;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.