Entity Framework --Entity SQL注意事项
Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM)。Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询。
l 关键字
Value关键字
ESQL 提供了 SELECT VALUE 子句以跳过隐式行构造。SELECT VALUE 子句中只能指定一项。在使用这样的子句时,将不会对 SELECT 子句中的项构造行包装器,并且可生成所要形状的集合,例如:SELECT VALUE it FROM NorthwindEntities.Customers as it
it关键字
it 出现在 ESQL 中, 查询对象的别名默认值 "it" 改成其他字符串,例如:
"SELECT VALUE it FROM NorthwindEntities.Customers as it " 。
l 注释:
Entity SQL 查询可以包含注释。注释行以两个短划线 (--) 开头。
"SELECT VALUE it FROM NorthwindEntities.Customers as it -- this a comment "
l Select查询
例如:
SELECT VALUE it FROM NorthwindEntities.Customers as it
l 参数
参数是在esql之外定义的变量,每个参数都有名称和类型,参数名称在查询表达式中定义,并以@符号作为前缀。例如:
Select VALUE c from NorthwindEntities.Customers as c where c.CustomerID=@customerID
l 聚合
Enity SQL不支持 * ,所以esql不支持count(*),而是使用count(0),例如:
Select count(0) from NorthwindEntities.Customers
l 分页SKIP/LIMIT
可以通过在 ORDER BY 子句中使用 SKIP 和 LIMIT 子子句执行物理分页。若要以确定的方式执行物理分页,应使用SKIP 和 LIMIT。如果您只是希望以非确定的方式限制结果中的行数,则应使用 TOP。TOP 和 SKIP/LIMIT 是互斥的
使用SKIP/LIMIT分页,esql代码如下:
Select value c from NorthwindEntities.Customers as c order by c.CustomerID skip 0 limit 10
l TOP
SELECT 子句可以在可选的 ALL/DISTINCT 修饰符之后具有可选的 TOP 子子句。TOP 子子句指定查询结果中将只返回第一组行。esql代码如下:
Select top(10) c.CustomerID from NorthwindEntities.Customers as c order by c.CustomerID
l NULL处理
Null 文本与 Entity SQL 类型系统中的任何类型都兼容,可以使用cast进行类型转换,例如:
select cast(c.region as string) from NorthwindEntities.Customers as c order by c.CustomerID limit 10
其中, Nvarchar等可以成string,数字类型可以转成int32,其他的类型转换类似。如果无法完成转换,则将报异常。还有可以处理的方法有treat。
l 标识符
Entity SQL 提供两种标识符:简单标识符和带引号的标识符
简单标识符:Entity SQL 中的简单标识符是字母数字和下划线字符的序列。标识符的第一个字符必须是字母字符(a-z 或 A-Z)。
带引号的标识符:带引号的标识符是括在方括号 ([]) 中的任何字符序列。带中文的部分,请使用方括号包括起来,否则会报如下异常信息:“简单标识符“中文”只能包含基本拉丁字符。若要使用UNICODE 字符,请使用转义标识符”
正确的代码如下:
Select c.CustomerID as [中文字符] from NorthwindEntities.Customers as c order by c.CustomerID skip 0 limit 10
l ROW
Esql可使用row来构建匿名的结构类型的纪录。例如:
SELECT VALUE row(p.ProductID as ProductID,p.ProductName as ProductName) FROM NorthwindEntities.Products as p order by p.ProductID LIMIT 10
l Key
提取引用或实体表达式的键。如下esql语句,直接返回Customer表的主键:
string esql = "SELECT value key(c) FROM NorthwindEntities.Customers as c order by c.CustomerID LIMIT 10"
l CreateRef/ref/deref
CreateRef创建对实体集中的实体的引用。
ref返回对实体实例的引用,之后就可以当作实体来访问其属性,esql语句如下:
SELECT ref(c).CustomerID FROM NorthwindEntities.Customers as c order by c.CustomerID LIMIT 10
deref运算符取消引用一个引用值,并生成该取消引用的结果。
l CASE语句:
string esql = "using SqlServer;select case when len(trim(c.CustomerID))==0 then true else false end from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
l 运算符
Esql支持的运算符有:加+、减-、乘*、除/、取模%、-负号。Esql语句如下:
select 100/2 as OP from NorthwindEntities.Customers as c order by c.CustomerID limit 10
l 比较运算符
Esql支持的比较运算符有:=,>,>=,IS [NOT] NULL,<,[NOT] BETWEEN,!=,<>,[NOT] LIKE。Esql语句如下:
select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 order by p.ProductID limit 10
l 逻辑运算符
Esql支持的逻辑运算符有:and(&&),not(!),or(||)。Esql语句如下:
select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 and p.UnitPrice<100 order by p.ProductID limit 10
或
select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 && p.UnitPrice<100 order by p.ProductID limit 10
l 字符串连接运算符。
加号 (+) 是 Entity SQL 中可将字符串串联起来的唯一运算符。Esql语句如下:
select c.CustomerID + c.ContactName from NorthwindEntities.Customers as c order by c.CustomerID limit 10
l 嵌套查询
在 Entity SQL 中,嵌套查询必须括在括号中,将不保留嵌套查询的顺序
select c1.CustomerID from( select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10) as c1
l 日期时间函数
Esql提供的日期时间函数有:CurrentDateTime()获取当前服务器的日期时间,还有month,day,year,second, Minute,Hour等。例如:
select CurrentDateTime() from NorthwindEntities.Customers as c order by c.CustomerID limit 10
l 字符串函数
Esql提供的字符串函数有:Concat,IndexOf,Left,Length,Ltrim,Replace,Reverse,Rtrim,SubString,Trim,ToLower,ToUpper.例如:
select Reverse(p.ProductName) as ProductName from NorthwindEntities.Products as p order by p.ProductID limit 10
l GUID
Esql提供newguid()函数,产生一个新的Guid。例如:
select newguid() from NorthwindEntities.Customers as c order by c.CustomerID limit 10
l 数学函数:
Abs,Ceiling,Floor,Round
l 统计函数:
Avg,BigCount,Count,Max,Min,StDev,Sum
l 位计算函数
如果提供 Null 输入,则这些函数返回 Null。这些函数的返回类型与参数类型相同。如果函数采用多个参数,则这些参数必须具有相同的类型。若要对不同类型执行位运算,则需要显式强制转换为相同类型.
BitWiseAnd,BitWiseNot,BitWiseOr,BitWiseXor
l 命名空间
Entity SQL 引入命名空间以避免全局标识符(如类型名称、实体集、函数等)出现名称冲突。Entity SQL 中的命名空间支持与 .NET Framework 中的命名空间支持类似。
Entity SQL 提供两种形式的 USING 子句:限定命名空间(其中,提供较短的别名以表示命名空间)和非限定命名空间,如下例所示:
USING System.Data;
USING tsql = System.Data;
例如:
string esql = "using System; select cast(p.UnitPrice as Int32) from NorthwindEntities.Products as p order by p.ProductID limit 10 ";
string esql = "using System;using SqlServer; select (cast(p.UnitPrice as Int32)),SqlServer.ltrim(p.ProductName) as nameLen from NorthwindEntities.Products as p order by p.ProductID limit 10 ";
最后,简单说一下Esql与T-Sql的某些差异:
l Entity SQL 中的所有列引用都必须用表别名限定.
l Esql不支持Any,all限定运算符以及*运算
l Entity SQL 当前未提供对 DML 语句(insert、update、delete)的支持。
l Entity SQL 的当前版本未提供对 DDL 的支持。
原文地址:http://blog.csdn.net/snzy521/article/details/5775844
Entity Framework --Entity SQL注意事项的更多相关文章
- Entity Framework执行Sql语句返回DataTable
Entity Framework中对外开放了数据库连接字符串,使用的时候可以直接得到这个连接字符串,然后进行相关的操作.如果在使用的过程中,发现Entity Framework中有一些满足不了的需求的 ...
- 一个最简单的使用Entity Framework 查询SQL 数据库的例子
1.ADO.NET 3.5 Entity Framework是随着.net framework 3.5一起发布的,确认开发环境版本是大于等于3.5版本 2.确认已经安装了ADO.NET 3.5 Ent ...
- [转]Entity Framework and SQL Azure
本文转自:https://msdn.microsoft.com/zh-cn/library/gg190738 Julie Lerman http://thedatafarm.com April 201 ...
- 配置Entity Framework连接Sql Server出现的一个异常
异常:The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFrame ...
- Entity Framework解决sql 条件拼接,完美解决 解决 不支持 LINQ 表达式节点类型“Invoke”【转】
传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...
- 如何处理Entity Framework / Entity Framework Core中的DbUpdateConcurrencyException异常(转载)
1. Concurrency的作用 场景有个修改用户的页面功能,我们有一条数据User, ID是1的这个User的年龄是20, 性别是female(数据库中的原始数据)正确的该User的年龄是25, ...
- Entity Framework关于SQL注入安全问题
1.EF生成的sql语句,用 parameter 进行传值,所以不会有sql注入问题 2.EF下有涉及外部输入参数传值的,禁止使用EF直接执行sql命令方式,使用实体 SQL 参考: https: ...
- Entity framework 级联删除注意事项
版本:EF6.0.1 RC 一对多场景,在子对象映射中开启级联删除情况下,删除父对象将自动删除其下所有子对象,需要注意一些事项: 需要保证DbContext中已经加载了该父对象的所有子对象. 如果Db ...
- Entity Framework 使用sql语句分页(查询视图)
1.查询视图 //3.查询视图 var sql = @" SELECT D.* FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY TestView.B_M ...
随机推荐
- 7.31实习培训日志-docker sql
docker mysql docker问题 今天问了老师有关docker的问题,entrypoint和cmd的区别,两者同时存在cmd的执行结果会被当做参数传入entrypoint. dockerfi ...
- hdu1083
#include"stdio.h" #include"string.h" #define N 305 int mark[N],link[N],map[N][N] ...
- 【lunux-make: *** 没有规则可以创建目标“install”。 停止。问题】
[root@localhost nginx-1.8.0]# make installmake: *** 没有规则可以创建目标“install”. 停止.
- Vue 全家桶单元测试简要指南
此文已由作者张汉锐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. vue 的单元测试环境 按照目前全家桶的情况,是需要 webpack 的支持的.可以直接使用 vue-cli ...
- [Windows]获取当前时间(年/月/日/时/分/秒)
struct tm* GetCurTime(time_t inTime) { struct tm* curTime = localtime(&inTime); curTime->tm_y ...
- JVM调试过程
一.查看系统情况 Linux查看CPU和内存使用情况 二.查看JVM启动参数 2.1 jcmd JVM诊断之查看运行参数
- poj 2068 Nim
Nim POJ - 2068 题目大意:多组数据,两人轮流操作,n轮一循环,给出总石子数和这n轮每次两人能取的石子上限(下限为1).取到最后一颗者输. /* f[i][j]表示在第i轮中一共有j个石子 ...
- AT2301 Solitaire
传送门 这里提供智障的\(O(n^2)\)做法 其实是有\(O(logn)\)做法的,但是我太菜了想不出来 Solution: 首先可以发现生成的序列一定是一个两边向中间单调递减的序列 这样就可以发现 ...
- CentOS编译安装GCC 4.9.2成功
在Linux上编译安装gcc是个寻烦恼的活,对于像我这样习惯于在Windows上面使用二进制安装包的人来说,自已编译安装gcc是个相当大的挑战,今天直接挑战最新版的gcc,是4.9.2版本的,做之前查 ...
- Vue中全局导入和按需导入的区别
export {router} //按需导出 import {router} from './router' //按需导入路由模块 export default //全局导出store模块 store ...