【发布时间】:2013-12-24 08:24:51
【问题描述】:
我正在关注本教程,但我不知道如何使用 Entity SQL: 或 Native SQL。有人可以帮忙吗?
http://www.entityframeworktutorial.net/Querying-with-EDM.aspx
当我尝试运行提供的代码时,在将浅蓝色文本更改为与我的项目匹配的内容后,我不断收到:
错误 1 'foo.PMEntities' 不包含 'ExecuteStoreCommand' 的定义,并且找不到扩展 > 方法 'ExecuteStoreCommand' 接受类型为 'foo.PMEntities' 的第一个参数(您是否>缺少 using 指令或程序集参考?)
ExecuteStoreQuery 和 CreateQuery 也有同样的问题。
我查看了我的参考资料,我认为应该没问题。
我可以用 linq 查询实体,但我不太习惯 Linq。
示例代码: `public ActionResult CharlieTest() { 使用 (var objCtx = new PMEntities()) { var schoolCourse = 来自 objCtx.Courses 中的 cs 其中 cs.CourseName == "Course1" 选择cs; //“课程”与课程相关 课程 mathCourse = schoolCourse.FirstOrDefault(); IList courseList = schoolCourse.ToList();
string courseName = mathCourse.CourseName;
}
//Querying with Object Services and Entity SQL
//Entity SQL does not recognize certain statements like createQuery and I can't add a reference to something that will help
//using (var objCtx = new PMEntities())
//{
// string sqlString = "SELECT VALUE cs FROM SchoolDBEntities.Courses AS cs WHERE cs.CourseName == 'Maths'";
// ObjectQuery<Cours> course = objCtx.
// <Cours>(sqlString);
// Cours coursename1 = course.FirstOrDefault<Cours>();
//}
//nativeSQL also doesn't work. Can not find ExecuteStoreCommand or ExecuteStoreQuery
using (var objCtx = new PMEntities())
{
//Inserting Student using ExecuteStoreCommand
int InsertedRows = objCtx.Database.SqlQuery("Insert into Student(StudentName,StandardId) values('StudentName1',1)").ToArray();
//Fetching student using ExecuteStoreQuery
var student = objCtx.Database.SqlQuery<Cours>("Select * from Student where StudentName = 'StudentName1'", null).ToList();
}
return View();
}`
大家平安夜快乐!
【问题讨论】:
-
你试过
ctx.Database.SqlQuery<T>()它和ExecuteStoreCommand几乎一样 -
我试过了,但我不确定有什么区别,我想准确地按照教程进行操作。是否有与 ExecuteStoreQuery 类似的方法?我在看这里,我什么也没看到。 msdn.microsoft.com/en-us/library/…
-
谢谢阿曼德。那成功了!如果有人需要示例代码,我只需将代码更改为
var student = objCtx.Database.SqlQuery<User>("SELECT * FROM [PM].[dbo].[Users] where Users.UserName = 'uguanch'").ToList();仍然需要弄清楚如何阅读内容
标签: c# visual-studio web-applications entity