【问题标题】:Having trouble with entity to make sql queries实体无法进行 sql 查询
【发布时间】: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&lt;T&gt;()它和ExecuteStoreCommand几乎一样
  • 我试过了,但我不确定有什么区别,我想准确地按照教程进行操作。是否有与 ExecuteStoreQuery 类似的方法?我在看这里,我什么也没看到。 msdn.microsoft.com/en-us/library/…
  • 谢谢阿曼德。那成功了!如果有人需要示例代码,我只需将代码更改为var student = objCtx.Database.SqlQuery&lt;User&gt;("SELECT * FROM [PM].[dbo].[Users] where Users.UserName = 'uguanch'").ToList(); 仍然需要弄清楚如何阅读内容

标签: c# visual-studio web-applications entity


【解决方案1】:

您需要为数据库上下文“foo”调用方法 ExecuteSqlCommand(query)(不适用于“foo.Pmentitis”)

例子:

   using (CompaniesEntities _c = new CompaniesEntities(
                extra.GetCompaniesConnectionString(
                    db_name
                )
            ))
        {

            _c.Database.ExecuteSqlCommand(@"
            if not exists (select * from sysobjects where name='agency_templates' and xtype='U')
                CREATE TABLE [dbo].[agency_templates](
                    [ag_templ_id] [int] IDENTITY(1,1) NOT NULL,
                    ...
            ", new object[] { });
   }

【讨论】:

  • 我正在这样做。
【解决方案2】:

您可能正在使用 Entity Framework 4,其中代码生成策略设置为“默认”,这会导致创建 ObjectContext。 VS2012 上的 Entity Framework 5 默认为数据模型创建一个 DbContext。 How to upgrade EF.

你也可以这样做:

int InsertedRows = (objCtx as IObjectContextAdapter).ObjectContext.ExecuteStoreCommand("Insert into students(StudentName,StandardId) values('StudentName1',1)", new object[] { });

【讨论】:

  • 没有。我在 EF 5 上,我刚刚更新到 6。它没有帮助,但谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多