TLDR; How do I read data from a table using Entity Framework, when the table name isn't known at compile-time?
There is an external system that processes a bulk of information, and then creates a new table for each batch run, and stores some data in that table. The column layout of these new tables are known before-hand, so I have generated an ADO.NET Entity Data Model (edmx file) from an existing database, where there is a table with the exact same column layout.
The original table in that database is called ResultTableTemplate, so the entity class representing that table is also called ResultTableTemplate.
I am trying to figure out how to use my ADO.NET Entity Data Model to read from those dynamically created tables, and getting back IEnumerable<ResultTableTemplate>. What I have done so far is this:
public IEnumerable<ResultTableTemplate> GetResultsFromTable(string tableName) {
using (var context = new WorkdataEntities()) {
var table = context.CreateQuery<ResultTableTemplate>("SELECT " +
"ALL_THOSE_COLUMN_NAMES... " +
"FROM " + tableName;
var query = from item in table select item;
return query.ToList();
}
}
When I run the query, I get a System.Data.EntitySqlException with the following message:
'ResultTable419828' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near member access expression, line 1, column 225.
ResultTable419828 is the value of tableName
I have tried tableName + " AS ResultTableTemplate" but it did not help.
Is there a way forward for me, or will I have to do this without the help of Entity Fr