如何在Entity Framework中访问context.Database.SqlQuery? [英] How to access context.Database.SqlQuery in Entity Framework?

查看:98
本文介绍了如何在Entity Framework中访问context.Database.SqlQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循本教程:

I am trying to follow this tutorial:

http://blogs.msdn.com/b/diego/archive/2012/01 /10/how-to-execute-stored-procedures-sqlquery-in-the-dbcontext-api.aspx

但似乎不能访问此方法,它不会显示在Intelli感觉

but can't seem to to access this method, it will not show up in intelli-sense

context.Database.SqlQuery

我正在使用以下代码,但似乎无法访问SqlQuery方法:

I am using the following code but can't seem to access the SqlQuery method:

using(Entities db = new Entities())
{    
}


推荐答案

您所描述的方法对于执行SQL与$ code> DbContext 版本的EF有效。 ( DbContext 用于Code First,也可以用于模型,但您需要做一些设置)。但是在你的例子中,应该如下。

The method you have described is valid for executing SQL vs a DbContext version of EF. (DbContext is used for Code First and is also available for model first but you need to do a little setup). However in your example it should be the following.

using(Entities db = new Entities())
{   
    db.Database.SqlQuery(....);
}

如果您首先使用OOB模型(即edmx),则可能使用一个 ObjectContext ,在这种情况下,您将需要执行以下操作:

If you are using OOB model first (ie edmx), you are probably using an ObjectContext, in which case you will need to perform the following:

using(Entities db = new Entities())
{   
    db.ExecuteStoreQuery<ReturnType>("...");
}

请参阅: http://blogs.microsoft.co.il /blogs/gilf/archive/2009/11/25/execute-t-sql-statements-in-entity-framework-4.aspx

这篇关于如何在Entity Framework中访问context.Database.SqlQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆