如何在Entity Framework中访问context.Database.SqlQuery? [英] How to access context.Database.SqlQuery in Entity Framework?
问题描述
我正在尝试遵循本教程:
I am trying to follow this tutorial:
但似乎不能访问此方法,它不会显示在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>("...");
}
这篇关于如何在Entity Framework中访问context.Database.SqlQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!