Entity framework raw SQL Query

前端 未结 1 565
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 10:10

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         


        
相关标签:
1条回答
  • 2020-11-30 10:44

    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");
    
    0 讨论(0)
提交回复
热议问题