Entity Framework Tutorial Basics(15):Querying with EDM
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的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- New Concept English three (55)
28w/m 45errors Recent developments in astronomy have made it possible to detect planets in our won M ...
- 学习动态性能表(14)--v$parameter&v$system_parameter
学习动态性能表 第14篇--V$PARAMETER&V$SYSTEM_PARAMETER 2007.6.11 这两个视图列出的各参数项名称以及参数值.V$PARAMETER显示执行查询的se ...
- VirtualBox-Debian7.2-share
1.在VirtualBox(用的版本是4.3.6)中,选择虚拟机->设置-> 共享文件夹, 2.添加一个主机上的文件夹readhat用来作为共享文件夹,选中固定分配和自动挂载, 结果:进入 ...
- 机器学习:SVM(基础理解)
一.基础理解 1)简介 SVM(Support Vector Machine):支撑向量机,既可以解决分类问题,又可以解决回归问题: SVM 算法可分为:Hard Margin SVM.Soft Ma ...
- linux下mysql配置文件my.cnf最详细解释
MySQL配置文件在Windows下叫my.ini,在MySQL的安装根目录下:在Linux下叫my.cnf,该文件位于/etc/my.cnf. 可以查找下:find / -name my.cnf m ...
- Dubbo入门之一:实例1
原文地址:http://blog.csdn.net/ruishenh/article/details/23180707?utm_source=tuicool 1. 概述 Dubbo是一个分布式服务 ...
- 实验吧CTF题库-编程(部分)
百米 3秒提交答案,数字是随机变化的 利用Python脚本解题 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import ...
- PDM生成数据库-0设置表名和字段名中不带双引号
如果PDM直接导出脚本的话,所有的表和字段都会被加上双引号,非常不方便,去除双引号的办法: Database->Edit Current DBMS在弹出窗体中第一项General中找到 Scri ...
- 第三章 Java程序优化(待续)
字符串优化处理 String对象及其特点 String对象是java语言中重要的数据类型,但它并不是Java的基本数据类型.在C语言中,对字符串的处理最通常的做法是使用char数组,但这种方式的弊端是 ...
- 记工作的变化--入住DB
2013年11月1日----一个值得纪念的日子! 今天才是我作为一个劳动者,步入社会的真正开始. 以前一直觉得做技术的技术做好就行了不用在意其余的细节.现实是做人(沟通)比做技术更重要! 以前一直觉得 ...