SQL

Entity Framework VS Stored Procedure

in this tutorial, you will learn what is important in terms of Development Time Or Performance between Entity Framework VS Stored Procedure.

Stored Procedure performed marginally better in overall.
Entity Framework is marginally slower but it is not as slow as making Stored Procedure a clear winner.
The first call for Entity Framework is significantly higher compare to the consecutive calls. If we were to exclude initial call in EF, EF will be a clear winner.
Choosing between Entity Framework or SP might not be straight forward especially if you have an existing application that has massive implementation in either approach. However here are some guidelines to make your decision making less painful.

  • If you have an existing system that has massive implementation on either approach, stick with the existing approach.
  • Having existing development team members who have strong background in stored procedure or entity framework respectively, continue to tap onto their strength.
  • If you are starting a new project, go for Entity Framework for ease of development and good maintainability although the cost to pay is slight performance downgrade.

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.

class Program  
   {  
       static void Main(string[] args)  
       {  
           int queries = 500;  
           Stopwatch spw = new Stopwatch();  
          //stored procedure code  
           spw.Start();  
           for (int i = 0; i < queries; i++)  
           {  
               using (var sqlConn = new SqlConnection("Data Source=.;Initial Catalog=School;Integrated Security=True"))  
               {  
                   var cmd = new SqlCommand("GetStudentsByName", sqlConn) { CommandType = CommandType.StoredProcedure };  
                   cmd.Parameters.AddWithValue("@StudentName", "Ramesh");  
                   sqlConn.Open();  
                   SqlDataReader dr = cmd.ExecuteReader();  
                   List<Students> Students = new List<Students>();  
                   while (dr.Read())  
                   {  
                       Students.Add(new Students { Students = dr.GetString(0) });  
                   }  
                   sqlConn.Close();  
               }  
           }  
           spw.Stop();  
           Console.WriteLine("Time taken by SP " + spw.ElapsedMilliseconds);  
           //Entity framework code  
           var context = new SchoolAppEntities();  
           var studentdet = context.Set<Student>();  
           spw.Start();  
           for (int i = 0; i < queries; i++)  
           {  
               Students students = studentdet.Where(x => x.StudentName == "Ramesh").First();  
           }  
          spw.Stop();  
          Console.WriteLine("Time taken by EF " + spw.ElapsedMilliseconds);  
          Console.ReadLine();  
       }  
   }  
   class Students  
   {  
       public string StudentName;  
   }  
}  

Result:

Time taken by SP 237

Time taken by EF 769

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button