Entity Framework Tutorial Basics(39):Raw SQL Query
Execute Native SQL Query
You can execute native raw SQL query against the database using DBContext. You can execute the following types of queries:
- SQL query for entity types which returns particular types of entities
- SQL query for non-entity types which returns a primitive data type
- 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=").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=");
//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="); }
Entity Framework Tutorial Basics(39):Raw SQL Query的更多相关文章
- Entity Framework Tutorial Basics(15):Querying with EDM
Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- Entity Framework Tutorial Basics(17):DBSet Class
DBSet Class DBSet class represents an entity set that is used for create, read, update, and delete o ...
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- Entity Framework Tutorial Basics(42):Colored Entity
Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- Entity Framework Tutorial Basics(36):Eager Loading
Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...
随机推荐
- uva11729 - Commando War(贪心)
贪心法,执行任务的时间J越长的应该越先交待.可以用相邻交换法证明正确性.其实对于两个人,要让总时间最短,就要让同一时间干两件事的时间最长. #include<iostream> #incl ...
- php 与 Smarty 中的 isset
今天在做一个分页模块的时候,在获取前端返回的当前页时,烦了一个大错!本来应该是这样子滴: 而咱却写成了这个样子: 在php里输出测试都没错,结果到Smarty里就被拒了! 测试结果是 这货跟谁都相等, ...
- 【剑指offer】以o(1)复杂度删除啊链表的节点,C++实现(链表)
0.简介 本文是牛客网<剑指offer>刷题笔记. 1.题目 在O(1)时间内删除链表节点. 2.思路 前提条件:删除的节点在链表上:边界条件:链表 ...
- input 标签中的checkd 添加与取消
//获取是否选中 var isChecked = $('#cb').prop('checked'); //或 var isChecked = $('#cb').is(":checked&qu ...
- [BZOJ1022][SHOI2008]小约翰的游戏
bzoj luogu sol 显然这个玩意儿和普通\(Nim\)游戏是有区别的. 形式化的,\(Nim\)游戏的关键在于决策集合为空者负,而这里的决策集合为空者胜. 所以就显然不能直接用\(SG\)函 ...
- LeetCode 315. Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- WebSphere & Log4j
IBM的东西,真是太麻烦了,一个日志都给你替换掉,太霸道了,Google了下,居然是个普遍想象,不过也有相关的解决方 案,基本好像都是在 WAS5,WAS6.1上的,我没环境,不过我这边的WAS7 没 ...
- php 字符串的分割
http://blog.sina.com.cn/s/blog_71ed1b870102uysa.html
- GWT异步更改cellTable中cell的数据显示
项目中遇到一个棘手的问题,使用GWT的cellTable的时候,要更改一个单元格的显示问题.如果仅仅是一个单独的cell 可能会有比较好的处理办法,比如可以找到这一列,然后更新整个cellTable, ...