Entity Framework already allows you to execute raw SQL queries and map their results to entities with SqlQuery. You don't need to use raw ADO.NET. You could just write:
public IEnumerable<T> LoadSomeLoads<T>(string table,int loadId)
{
using (ReconContext context = new ReconContext())
{
var sq;="SELECT * FROM "+ table+ " WHERE LoadId = @p0";
var results= context.SqlQuery<T>(sql,loadId)
.ToArray();
}
}
You should validate the table
parameter to ensure it's an allowed table name, otherwise someone could query tables he has no authorization to, or simply browse for data by forcing the table name.
You avoid catastrophic injection attacks by using parameters but that doesn't prevent unauthorized disclosure if someone can force a query to an inappropriate table.
I suspect though that you should be looking to table-per-type inheritance. It sounds like you want to load different classes from different tables, where at least one property (LoadId) is common.
With Table-per-Type EF maps different types of an inheritance hierarchy to different tables and selects the correct table based on the mapping configuration.
Assuming, eg that you have the following classes:
public abstract class LoadTypeBase
{
public int LoadId { get; set; }
...
}
[Table("LoadTableA")]
public class LoadTypeA : LoadTypeBase
{
...
}
[Table("LoadTableB")]
public class LoadTypeA : LoadTypeBase
{
...
}
and this context
public class ReconContext: DbContext
{
...
public DbSet<LoadTypeBase> Loads { get; set; }
}
You could query for all load types with a specific LoadId with :
var query = from someLoad in context.Loads
where someLoad.LoadId = loadId
select someLoad;
You can restrict results to a specific type with OfType<T>()
var query = from someLoad in context.Loads.OfType<LoadTypeB>()
where someLoad.LoadId = loadId
select someLoad;