Querying with EDM:

We have created EDM, DbContext, and entity classes in the previous sections. Here, you will learn the different types of queries an entity framework supports, which is in turn converted into SQL query for the underlaying database.

Entity framework supports three types of queries: 1) LINQ to Entities, 2) Entity SQL, and 3) Native SQL

LINQ to Entities:

Language-Integrated Query (LINQ) is a powerful query language introduced in Visual Studio 2008. You can use LINQ in C# or Visual Basic to query different data sources. LINQ-to-Entities operates on entity framework entities to access the data from the underlying database. You can use LINQ method syntax or query syntax when querying with EDM. Visit LINQ Tutorials to learn LINQ step-by-step.

LINQ Method syntax:

//Querying with LINQ to Entities
using (var context = new SchoolDBEntities())
{
var L2EQuery = context.Students.where(s => s.StudentName == "Bill"); var student = L2EQuery.FirstOrDefault<Student>(); }

LINQ Query syntax:

using (var context = new SchoolDBEntities())
{
var L2EQuery = from st in context.Students
where st.StudentName == "Bill"
select st; var student = L2EQuery.FirstOrDefault<Student>();
}

First, you have to create an object of context class, which is SchoolDBEntities. You should initialize it in using() so that once it goes out of scope then it will automatically call Dispose() method of DbContext. In both the syntaxes above, context returns IQueryable.

Learn different types of LINQ to Entities projection query in the Projection Query section.

Entity SQL:

Entity SQL is another way to create a query. It is processed by the Entity Framework’s Object Services directly. It returns ObjectQuery instead of IQueryable.

You need ObjectContext to create a query using Entity SQL.

The following code snippet shows the same query result as L2E query above.

//Querying with Object Services and Entity SQL
string sqlString = "SELECT VALUE st FROM SchoolDBEntities.Students " +
"AS st WHERE st.StudentName == 'Bill'"; var objctx = (ctx as IObjectContextAdapter).ObjectContext; ObjectQuery<Student> student = objctx.CreateQuery<Student>(sqlString);
Student newStudent = student.First<Student>();

You can also use EntityConnection and EntityCommand to execute Entity SQL as shown below:

using (var con = new EntityConnection("name=SchoolDBEntities"))
{
con.Open();
EntityCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT VALUE st FROM SchoolDBEntities.Students as st where st.StudentName='Bill'";
Dictionary<int, string> dict = new Dictionary<int, string>();
using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection))
{
while (rdr.Read())
{
int a = rdr.GetInt32();
var b = rdr.GetString();
dict.Add(a, b);
}
}
}

EntityDataReader doesn't return ObjectQuery. Instead, it returns the data in rows & columns.

Visit MSDN blog to learn Entity SQL.

Native SQL:

You can execute native SQL queries for a relational database as shown below:

using (var ctx = new SchoolDBEntities())
{
var studentName = ctx.Students.SqlQuery("Select studentid, studentname, standardId from Student where studentname='Bill'").FirstOrDefault<Student>();
}

Learn to execute raw sql query using DbContext in Raw SQL Query section.

Entity Framework Tutorial Basics(15):Querying with EDM的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  2. 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 ...

  3. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

  4. 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 ...

  5. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  6. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

  7. 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 ...

  8. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

  9. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

随机推荐

  1. grunt-2x2x

    a grunt plugin to resize and rename @2x.png(jpg,gif,) image to .png(jpg,gif) 场景:移动前端开发中,设计给的psd都是双倍图 ...

  2. leetcode_sql_1,176,177

    1.176题目,Second Highest Salary,https://leetcode.com/problems/second-highest-salary/#/description Writ ...

  3. Scala 面向接口编程

    1.如果要实现一个接口,前边没有extends关键字就可以使用extends,如果有要使用with关键字 2.Scala 中的接口支持多种继承,类或者抽象类不支持多种继承 3.抽象属性:未被实例化的属 ...

  4. 关于overflow:hidden

    (本文只针对hidden这个值的用处进行阐述) 关于overflow:hidden;很多人都知道他是溢出隐藏的一个属性,但是并不是很多人知道它的一些神奇的地方!首先先讲一下众所周知的溢出隐藏吧! 溢出 ...

  5. [BZOJ4573][ZJOI2016]大♂森林

    bzoj luogu uoj sol \(orz\ \ HJT\ \ dalao\)教会我做这道题. 考虑每两个相邻位置的树的差异. 对于一个1操作(更换生长节点),假设区间是\([l,r]\),那么 ...

  6. JavaScript6 新语法 let 有什么优势

    最近看国外的前端代码时,发现ES6的新特性已经相当普及,尤其是 let,应用非常普遍 虽然 let 的用法与 var 相同,但不管是语法语义上,还是性能上,都提升了很多,下面就从这两方面对比一下 语法 ...

  7. 最终还是选择了markdownpad2

    markdownpad2使用 最终 哈哈,最后还是选择了markdownpad2,经过探索才知道这个玩意多么好用. 点击,下载. 碰到的问题 1.win10出现HTML无法渲染得对话框 结果是,官网有 ...

  8. Chroma Oracle 安装宝典,吐血整理

    尼玛,太坑爹!安装: 1.Chroma Application Service 2. PL SQL 安装Oracle 11g 的步骤和过程: 第一步:只能安装 Oracle 11g 64 bit 数据 ...

  9. Win8 安装.Net Framework3.5(2.0,3.0)组件二种方式

    第一种: 通过命令+win8映像文件 找到系统盘cmd文件:C:\WINDOWS\system32\Cmd.exe 右键“以管理员身份运行”,然后弹出一个黑框框. 黑框框里面输入一下命令: dism. ...

  10. Unix文件指令-Mac终端命令应用

    pwd:查看当前文件夹 cd: 打开文件夹 ls:列出当前路径下所有文件 ls -l :列出当前路径下的所有文件详细信息. mkdir: 新建文件夹 touch: 创建文件   eg: touch t ...