Which one gives better performance? ADO.NET or Entity Framework.
These are the two method I want to analyze.
ADO.NET Test Method
public void ADOTest() { Stopwatch stopwatch = Stopwatch.StartNew(); using (SqlConnection con = new SqlConnection(connection)) { string Query = "select * from Product "; SqlDataAdapter da = new SqlDataAdapter(Query, con); DataSet ds = new DataSet(); con.Open(); da.Fill(ds); DataView dv = ds.Tables[0].DefaultView; } stopwatch.Stop(); Console.WriteLine("ADO.NET Time Elapsed={0}", stopwatch.Elapsed); }
Entity Framework Test Method
public void EFTest() { Stopwatch stopwatch = Stopwatch.StartNew(); var list = _OnlineStoreEntities.Products.ToList(); stopwatch.Stop(); Console.WriteLine("Entity Framework Elapsed={0}", stopwatch.Elapsed); }
Result in first time execution
When I ran this above method in more than 100 times. The average execution time is shown in the image:
ADO.NET took only 2 milliseconds whether Entity Framework took more than 4 milliseconds.
Result in second time execution
When I ran this method again and again in single run. The average execution time between ADO.NET and EF is not much more:
Question
In this article, we conclude that the ADO.net and the entity framework has a difference in performance as ADO.net is faster in performance, and we have also seen some key differences between them related to how they are flexible, how data provider work, the maintainable code, and also the speed of development.
If we want to achieve more control over SQL commands and operations with the help of raw SQL Queries, then ADO.NET will be a great choice to start work with. Whereas if we want to develop the application in a much faster way with clear code maintainability, then Entity Framework will be the better choice.
The AsNoTracking method tells Entity Framework to stop that additional work and so, it can improve the performance of your application. So, in theory, a query with AsNoTracking should perform better than without.
EF is built on top of ADO.Net, meaning that you can use both at the same time.
Take a look on msdn article Performance Considerations (Entity Framework)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With