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 ...
随机推荐
- SQL-表的操作(创建表,删除表,更改列,插入新行,更改行的值,删除表中数据)
一,操作表及列 1.创建表: CREATE TABLE test (ID int PRIMARY KEY IDENTITY,Name varchar(20) ) 2.删除表 DROP TABLE t ...
- JSDoc 介绍
什么是JSDoc JSDoc是一个根据javascript文件中注释信息,生成JavaScript应用程序或库.模块的API文档 的工具.你可以使用他记录如:命名空间,类,方法,方法参数等.类似Jav ...
- Yii 常用命令
一.Yii的Active Recorder包装了很多. 特别是把SQL中 把where,order,limit,IN/not IN,like等常用短句都包含进CDbCriteria这个类中去,这样整个 ...
- Phong光照模型的Shader实现
计算反射向量 Phong用到的是反射向量,计算反射向量的公式是 R = 2*N(dot(N, L)) - L 这个公式是根据向量的投影公式以及平行四边形法则推导出来的 详细步骤请看这篇文章,讲的非常好 ...
- 查看.Net Framework版本号
目录 摘要 .NET Framework 的版本 确定计算机上安装的 .NET Framework 版本 补充几个查看.Net Framework版本号 概要 本文描述如何确定计算机上安装的 Micr ...
- python basestring()
作用: basestring是str和unicode的超类(父类),也是抽象类,因此不能被调用和实例化,但可以被用来判断一个对象是否为str或者unicode的实例,isinstance(obj, b ...
- SQL Server 学习系列之五
SQL Server 学习系列之五 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题) SQL Server 学习系列之三(SQL 关键字) SQ ...
- Road to OI
我学OI已经三年有余了.回首向来萧瑟处,在镜花水月一般的OI生涯面前,我不敢,也没资格称“也无风雨也无晴”.这三年我过得浑浑噩噩,玩了很多游戏,看了很多番,追过一个女孩,OI却搞得一塌糊涂.留给我的时 ...
- IHE 官方网址有用资源介绍
实现标准: http://www.ihe.net/Technical_Frameworks/ 各个实现框架文档, 比如XDS,XCA,PIX,PDQ等 测试工具:http://www.ihe.net/ ...
- BMP格式转JPEG格式
int Bmp2Jpg(const char *bmp_data, const char *jeg_file, const int width, const int height) { int ret ...