实体框架原始 SQL 查询 [英] Entity framework raw SQL Query
本文介绍了实体框架原始 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从数据库中选择多个列,但我没有匹配的实体.所以我的查询看起来像这样:
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");
我正在获取结果集,每行包含字符串列表,但计数为 0.
I am getting the result set, each row contains list of strings but count is 0.
那么如何使用 Database.SqlQuery
选择多列?
So how do I select multiple columns using Database.SqlQuery
?
推荐答案
您必须将结果捕获到具有匹配属性名称的类中,以及(至少)一个无参数构造函数:
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");
这篇关于实体框架原始 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文