简体   繁体   中英

Entity framework sqlquery

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

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM