Entity Framework Tutorial Basics(39):Raw SQL Query

來源:互聯網
上載者:User

標籤:

Execute Native SQL Query

You can execute native raw SQL query against the database using DBContext. You can execute the following types of queries:

  1. SQL query for entity types which returns particular types of entities
  2. SQL query for non-entity types which returns a primitive data type
  3. Raw SQL commands to the database
SQL query for entity types:

As we have seen in one of the previous chapters, DBSet has SQLQuery() method to write raw SQL queries which return entity instances. The returned objects will be tracked by the context, just as they would be if they were returned by a LINQ query. For example:

using (var ctx = new SchoolDBEntities()){    var studentList = ctx.Students.SqlQuery("Select * from Student").ToList<Student>();  }

 

However, columns returned by SQL query should match the property of an entity type of DBSet otherwise, it will throw an exception. For example:

using (var ctx = new  SchoolDBEntities()){                    var studentName = ctx.Students.SqlQuery("Select studentid, studentname         from Student where studentname=‘New Student1‘").ToList();}

 

If you change the column name in query, then it will throw an exception because it must match column names:

using (var ctx = new SchoolDBEntities()){                    //this will throw an exception    var studentName = ctx.Students.SqlQuery("Select studentid as id, studentname as name             from Student where studentname=‘New Student1‘").ToList();}

 

SQL query for non-entity types:

A SQL query returning instances of any type, including primitive types, can be created using the SqlQuery method on the Database class. For example:

using (var ctx = new SchoolDBEntities()){    //Get student name of string type    string studentName = ctx.Database.SqlQuery<string>("Select studentname         from Student where studentid=1").FirstOrDefault<string>();}

 

Raw SQL commands to the database:

ExecuteSqlCommnad method is useful in sending non-query commands to the database, such as the Insert, Update or Delete command. For example:

using (var ctx = new SchoolDBEntities()){    //Update command    int noOfRowUpdated = ctx.Database.ExecuteSqlCommand("Update student             set studentname =‘changed student by command‘ where studentid=1");    //Insert command    int noOfRowInserted = ctx.Database.ExecuteSqlCommand("insert into student(studentname)             values(‘New Student‘)");    //Delete command    int noOfRowDeleted = ctx.Database.ExecuteSqlCommand("delete from student             where studentid=1");}

 

Entity Framework Tutorial Basics(39):Raw SQL Query

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.