In an existing system application which I am working on, we need to decide whether we should use entity framework or stored procedure to optimize the performance of our application.
I am not a big fan of Entity Framework but still, I know some of its good features.
Entity Framework uses LINQ which provides a fantastic feature to the developers, like – compile type error checking, IntelliSense at development time, common queries across the databases etc. All these features provide quick development turnaround time.
However, I was not sure whether it provides good performance in comparison to the stored procedure or not. So, I developed a simple console application to compare the results.
I did the same operation once by using EF and the second time with a stored procedure and noted the execution time both times.
Below is my console application followed by the result.
- class Program
- {
- static void Main(string[] args)
- {
- int queries = 500;
- Stopwatch spStopwatch = new Stopwatch();
- //stored procedure code
- spStopwatch.Start();
- for (int i = 0; i < queries; i++)
- {
- using (var sqlConn = new SqlConnection("Data Source=NJO-LPT-GJAIN;Initial Catalog=BookDb;Integrated Security=True"))
- {
- var cmd = new SqlCommand("searchBook", sqlConn) { CommandType = CommandType.StoredProcedure };
- cmd.Parameters.AddWithValue("@Bookname", "java");
- sqlConn.Open();
- SqlDataReader dr = cmd.ExecuteReader();
- List<book> books = new List<book>();
- while (dr.Read())
- {
- books.Add(new book { BookTitle = dr.GetString(0) });
- }
- sqlConn.Close();
- }
- }
- spStopwatch.Stop();
- Console.WriteLine("Time taken by SP " + spStopwatch.ElapsedMilliseconds);
- //Entity framework code
- var context = new BookDbEntities();
- var bookset = context.Set<Book>();
- spStopwatch.Start();
- for (int i = 0; i < queries; i++)
- {
- Book books = bookset.Where(x => x.BookTitle == "java").First();
- }
- spStopwatch.Stop();
- Console.WriteLine("Time taken by EF " + spStopwatch.ElapsedMilliseconds);
- Console.ReadLine();
- }
- }
- class book
- {
- public string BookTitle;
- }
- }
My opinion is that Entity Framework provides a very good feature but can’t beat the performance of the stored procedure because of its precompiled nature.
Please let me know if you have a different opinion.

Imran HassanPosted Nov 26, 2019, 11:14 AM
Can I use hybrid model...means use procedures with EF....
Vaibhav AgarwalPosted Jul 12, 2019, 11:44 AM
Dapper is best. It gives best of both worlds
Teddy DuriPosted Jul 8, 2019, 4:18 AM
Nice article, please also do a review on Dapper l know it is faster than EF.
Sumit ShekhawatPosted Mar 7, 2019, 4:40 PM
That's a good topic to best practice. Nice article Gaurav ji. :)
Salman BegPosted Feb 28, 2019, 8:31 AM
You can use EF with Stored Procedure but that also take some time. Dapper is also best. Thanks.
David BrenchleyPosted Feb 27, 2019, 9:54 AM
How does entity framework core compare?
Suraj KumarPosted Feb 27, 2019, 6:41 AM
Good thing shared.
Mithun PattankarPosted Feb 26, 2019, 12:30 PM
I would rather use EF with stored procedure together. As EF can call SPs, it would help to get heavy duty retrieval with the typed result set i.e. joining multiple tables with a large amount of data. Also using EF for Insert/ Update/ Delete would save time, track changes, get transaction support. I used mixture of EF with SPs when needed.