In this article, I am going to discuss Querying in Entity Framework. Please read our previous article where we discussed how to perform CRUD Operations in Entity Framework Database First Approach.

Note: We are going to work with the same example that we created in our Introduction to Entity Framework Database First Video. Please read our introduction to Entity Framework Database First Video before proceeding to this Video.
Different Approaches for Querying in Entity Framework
In Entity Framework, you can build and execute queries in three different ways to fetch the data from the underlying database. They are as follows:
- LINQ-to-Entities
- Entity SQL
- Native SQL
LINQ-to-Entities Queries in Entity Framework:
The Language-Integrated Query (LINQ) is a powerful query language that was introduced with .Net 3.5 and Visual Studio 2008. You can use LINQ with C# or VB to query different types of data sources such as SQL, XML, In memory objects, etc.
As the name suggests, the LINQ-to-Entities queries operate on the entity set i.e. DbSet type properties to access the data from the underlying database. You can use both method and query syntax when querying the entities. Visit our LINQ Tutorials to learn LINQ in detail.
Example: LINQ-to-Entities Queries using Method syntax
In the following example, the LINQ-to-Entities query uses LINQ Method Syntax to fetches the data from the Student table where the StudentId is 1.
Output: FirstName: Virat, LastName: Kohli
Example: LINQ-to-Entities Queries using Query syntax
Output: FirstName: Virat, LastName: Kohli
Note: As you can see in the above two examples, we created an instance of the context class EF_Demo_DBEntities i.e. the class which is inherited from the DbContext class. It is recommended to instantiate the DbContext class using the using() block so that once it goes out of the scope, it automatically gets disposed. In our next Video, we will discuss the different ways to use LINQ-to-Entities queries.
Entity SQL Queries in Entity Framework
Entity SQL is another approach to write a query. It is processed by the Entity Frameworks Object Services directly. It returns ObjectQuery instead of IQueryable. In this case, you need an ObjectContext to create a query using Entity SQL. The following example shows how to use the Entity SQL Query to fetch the data from the underlying database.
You can also use EntityConnection and EntityCommand to execute Entity SQL queries as shown in the below example.
Output: FirstName: Virat, LastName: Kohli
Native SQL Queries in Entity Framework:
The Entity Framework allows us to execute Native SQL queries for the underlying database. The following three methods are used to execute raw SQL queries in Entity Framework/
- DbSet.SqlQuery()
- DbContext.Database.SqlQuery()
- DbContext.Database.ExecuteSqlCommand()
DbSet.SqlQuery()
The SqlQuery method on DbSet allows us to write and execute Native SQL query in Entity Framework that will return entity instances. The returned objects will be tracked by the context. The following example returns all the students from the database.
In the above example, the Native SQL Query “Select * from Student†will execute in the database to get all students, and then it will be converted into a list of Student entities. The column names in the SQL query must match with the properties of an entity type, otherwise, it will throw an exception. When you execute the above program, you will get the following output.
You can specify the parameters using the object of SqlParameter, as shown in the below example.
Note: If you change the column name in the SQL query, then it will throw an exception because the column names must be matched with entity properties. The following example will throw an exception.
Output:
The DbSet<TEntity>.SqlQuery() method in Entity Framework executes only the Native SQL for the table which is mapped with the specified entity and only returns the result from the corresponding database table and not from any other table. The following will throw an exception.
In the above example, the DbSet class Student is used and we are trying the access the data from the Course. And hence when you execute the above program, you will get the following exception.
SQL Query for Non-entity Types
The SQL query returning instances of any type, including the primitive types, can be created using the SqlQuery method on the Database class. For example, in the code, we are returning only the FirstName which is of type string.
Database.SqlQuery()
The Database class represents the underlying database and provides various methods to deal with the database. The Database.SqlQuery() method returns a value of any type.
Output: FirstName: Virat, LastName: Kohli
Database.ExecuteSqlCommand()
The Database.ExecuteSqlCommnad() method is useful in executing database DML commands, such as the Insert, Update, and Delete commands.
Note: Among the three approaches the most preferred approach which most of the developers are used is LINQ-to-Entities. If you are new to LINQ queries, then I recommended you to read our LINQ Course where we explain everything about LINQ.