0

I read about querying database using the entity framework

var result = _dbContext.SqlQuery<string>(sql, someParamSqlParameter).ToList();

What if i wanted multiple columns to be returned how could i write that type of query. I tried this code but it gives some sql schema mapping error

var result = clsGlobalObjectRefrances.SchoolSoulController.Stt.Database.SqlQuery<LocalAccGroups>(sqlQuery).ToList();
var sqlQuery = "Select GroupId,GroupName,Level from cte_AccGroups";

Where LocalAccGroups is a class i created

class LocalAccGroups
    {
        public decimal GroupId { get; set; }
        public string GroupName { get; set; }
        int Level { get; set; }
    }

Thanxxx in Advance

7
  • 2
    What are the columns returned by your query ? If it is returning Level, then you should mark it as public in your class Commented Oct 10, 2013 at 13:31
  • It's pretty important to post your query and the definition of your table in the database. The column names and types returned by your query need to be identical to the object you are deserializing them into. Commented Oct 10, 2013 at 13:32
  • Why you not just try to do var result = _dbContext.SqlQuery<LocalAccGroups>(sql, someParamSqlParameter).ToList(); !? Commented Oct 10, 2013 at 13:33
  • Ok wait i am editing my question and giving the query i used Commented Oct 10, 2013 at 13:35
  • @Habib is there any different between DBContext.SqlQuery and Database.SqlQuery ? Commented Oct 10, 2013 at 13:37

1 Answer 1

1

Your query is returning Level as well, and you haven't marked your property Level in your class as public. Mark your property as public and it should be good. Also make sure that the data type matches the one returned by query. It seems odd go a GroupId to be of type decimal.

class LocalAccGroups
{
    public decimal GroupId { get; set; }
    public string GroupName { get; set; }
    public int Level { get; set; }
}
Sign up to request clarification or add additional context in comments.

6 Comments

class should be also public ?
then what should i declare groupId as
@BassamAlugili, not really, if both the class and query are in the same namespace.
@MurtazaMunshi, check the data type in database, Go to table, Column definition and you will see the data type for GroupId, chances are that it is of type int
Yeaa i checked it out its numeric(18,0). And i tried the code you just suggested but its giving me this error: Schema specified is not valid. Errors: The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'AccLedger'. Previously found CLR type 'SchoolSoulLibrary.AccLedger', newly found CLR type 'Accounts.Method.AccLedger'.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.