Native sql using entity framework without entity class

1k Views Asked by At

I have a native sql query which changes dynamically based on user selection.
Code

var sql = "select * from " + temp + ";";
var templist = db.Database.SqlQuery<>(sql).ToList();

The temp variable contains table name which is obtained based on user selection. how can i fetch the records without using entity class in between.sqlquery<>?. Is there any other way to fetch records?.

Note: Dynamic tables are not available in entity model.It is only available in db

1

There are 1 best solutions below

0
On

Only substitution classes

class A1 {public int Id {get; set;}}   
class A2 {public string Name {get; set;}}  
...

var templist = db.Database.SqlQuery<A1>(sql_dinamic).ToList(); 
or 
var templist = db.Database.SqlQuery<A2>(sql_dinamic).ToList(); 

Read the class factory!