有实体制作SQL查询的麻烦

时间:2013-12-24 08:24:51

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

我正在学习本教程,但我不知道如何使用Entity SQL:或Native SQL。有人可以帮忙吗?

http://www.entityframeworktutorial.net/Querying-with-EDM.aspx

当我尝试运行提供的代码时,在将浅蓝色文本更改为与我的项目匹配的内容后,我不断获取:

  

错误1'foo.PMEntities'不包含'ExecuteStoreCommand'的定义,并且没有扩展>方法'ExecuteStoreCommand'接受类型'foo.PMEntities'的第一个参数可以找到(你是否>缺少使用指令或程序集引用?)

ExecuteStoreQuery和CreateQuery也存在同样的问题。

我查看了我的参考文献,我认为事情应该没问题。

a busy cat http://img707.imageshack.us/img707/8615/8nz8.png

我可以用linq查询实体,但我不太习惯Linq。

示例代码: `public ActionResult CharlieTest()         {             使用(var objCtx = new PMEntities())             {                 var schoolCourse =来自objCtx.Courses中的cs                                    其中cs.CourseName ==“Course1”                                    选择cs;                 //“Cours”与课程相关                 Cours 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();
    }`
圣诞平安夜大家!

2 个答案:

答案 0 :(得分:1)

你需要为db context“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[] { });
   }

答案 1 :(得分:0)

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

你也可以这样做:

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