Entity framework raw SQL Query

.net c# entity-framework

Question

I have to select multiple columns from a database and I don't have a matching entity. so my query looks like this:

var result = _dbContext.Database.SqlQuery<List<string>>(
             "select ID, NAME, DB_FIELD from eis_hierarchy");

I am getting the result set, each row contains list of strings but count is 0.

So how do I select multiple columns using Database.SqlQuery?

1
20
6/3/2014 8:21:39 AM

Accepted Answer

You have to capture the results into a class with matching property names, and (at least) a parameterless constructor:

class DbResult
{
    public int ID { get; set; }
    public string NAME { get; set; }
    public string DB_FIELD { get; set; }
}

var result = _dbContext.Database.SqlQuery<DbResult>(
                 "select ID, NAME, DB_FIELD from eis_hierarchy");
46
1/20/2015 1:50:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow