实体框架以IQueryable <t> </t>的形式返回SQL的任何方式

时间:2014-05-23 00:38:52

标签: c# entity-framework asp.net-mvc-5 entity-framework-6 entity-framework-6.1

假设正在使用EF6.1 ....

假设'Category'是EF持久存在的POCO类型,'context'有一个名为'categories'的DbSet属性,'x'被定义为int类型的合法本地,而c.ID也是类型为int .....

给出以下表达式

context.Categories.Where<Category>((Category c) => {return c.ID == x});

在查询发生之前,有没有办法将编译后的SQL语句作为字符串返回而不会访问数据库?

1 个答案:

答案 0 :(得分:1)

public IQueryable<Category> GetCategory(int x)
{
   return context.Categories.Where<Category>(c => c.ID == x);
}

要查看SQL:

Console.WriteLine(GetCategory(1).ToString());