Entity Framework is also a recommended option to build any complex applications. It is more powerful than LINQ to SQL and ADO.NET. Object relational mapping (ORM) is the main aspect that entity framework is concerned with, which will generate entity model objects according to database structure, also reduce programmer code and is very simple to use. Developer doesn’t need to write SQL statements to access data.
Entity framework provides an alternative method for querying an Entity data model (EDM), if you’re working in a language that doesn’t support Language integrated query (LINQ) or simply prefer to use syntax that’s closer to SQL. It’s on the object context.
The syntax of entity SQL is almost identical to standard SQL. It supports all the clauses you’d expect, including FROM,WHERE, JOIN and ORDER BY etc., but there is a difference in the SELECT clause, which determines what kind of results the query returns.
It provides a natural transition for SQL developers. While Entity SQL was heavily influenced by SQL, Entity SQL is not pure SQL. Let us look at some of the differences in this blog. It is worth mentioning that Entity SQL does not prevent one from writing classic SQL queries as we will see a sample below on how to navigate relationships using classic joins/foreign-keys. This blog also shows how Entity SQL supports, relationships to simplify this common task.
Entity SQL
A query expression must begin with a ‘FROM’ clause. Additionally, a query expression can contain subqueries, which also begin with ‘FROM’ clause. The ‘FROM’ clause specifies the following:
- The data source on which the query or subquery will be run.
- A local range variable that represents each element in the source sequence.
Both the range variable and the data source are strongly typed. The data source referenced in the FROM clause must have a type of IEnumerable , IEnumerable <T> , or a derived type such as IQueryable<T>.
Actually in the Entity SQL FROM clause acts as a foreach loop in C#, it will create the object to our entity model, by using the object we can get selected values or all the values of model.
Comparison between SQL and Entity Framework
SQL | Entity Framework |
While getting data from database, it always requires queries to start with SELECT clause. | While getting data from database it always requires queries to start with FROM clause. |
Different types of joins statement like INNER JOIN, LEFT JOIN and RIGHT JOIN are supported. | To join two or more, we use JOIN keyword, we have only one join keyword by default it is an INNER JOIN |
The output of select query returns data-table. | The output of select query returns IEnumerable |
All the queries are precompiled | Queries are not precompiled. |
It supports WHERE, GROUP BY, and ORDER BY etc., | It supports WHERE, GROUP BY and ORDER BY etc., |
Example
In this example, let us learn how to get the data from the Employee table using SQL and Entity SQL.
SQL: SELECT * FROM TBL_EMPLOYEE
The above query returns all the employees from employee table and it will return the DataTable.
Entity SQL based: FROM emp in TBL_EMPLOYEE SELECT emp
In the Entity SQL, the emp acts as an object, it will return the IEnumerable Employee List.
JOINS
In SQL we have different types of Joins like INNER JOIN, LEFT OUTER JOIN and RIGHT OUTER JOIN, but in entity SQL we have only JOIN keyword by default it acts as INNER JOIN in SQL.
Example
In this example, we will learn the difference between SQL JOINS and Entity SQL JOINS. Let us consider we have two tables TBL_EMPLOYEE contains the employee details (EmpId, Name, Email Id, Contact NO) table twoTBL_DEPARTMENTINFO contains employee job details (DeptId, EmpId, DeptName). To get employee and his corresponding department we need joins.
SQL based:
SELECT e.NAME, d.DeptName FROM TBL_EMPLOYEE e INNER JOIN
TBL_DEPARTMENTINFO d on e.EmpId = d.EmpId
Entity SQL based:
Var EmpLst = ( FROM emp in TBL_EMPLOYEE
JOIN dept in TBL_DEPARTMENTINFO on
emp.EmpId == dept.EmpId
SELECT new {emp.NAME, dept.DeptName}).ToList();
In the above example, we have replaced the INNER JOIN clause with JOIN in Entity SQL query.
To know more about the Entity Frame work @
Phenomecloud is an enthusiastic family of individuals, fervent to make lives simpler through effective use of technology. Our mission is to implement solutions that drives business results. Know more insights from our thoughts and experience.
Contact us today or call 1-855-978-6816 to talk with us about your business needs.
Leave a Comment