What is the difference in performance when using the query methods the base SqlQuery<TElement>
and ExecuteSqlCommand
of EntityFramework in relation to directly using ADO.NET ?
If there is a significant difference in performance, this is due to data processing performed by the EntityFramework before accessing the database or EntityFramework also causes impacts on database?
Using ADO.NET to perform a select
using (SqlConnection connection = new SqlConnection("connectionString"))
{
using (SqlCommand command = new SqlCommand("SELECT * FROM TABLE", connection))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
///...
}
}
}
Using Entity Framework to perform a select with SQLQuery
context.Database.SqlQuery<Table>("SELECT * FROM TABLE");
Using ADO.NET to perform an insert statement
using (SqlConnection connection = new SqlConnection("connectionString"))
{
using (SqlCommand command = new SqlCommand("INSERT INTO TABLE VALUES ('foo', 'ba', GETDATE())", connection))
{
int records = command.ExecuteNonQuery();
}
}
Using EntityFramework to perform an insert statement with ExecuteSqlCommand
context.Database.ExecuteSqlCommand("INSERT INTO TABLE VALUES ('foo', 'ba', GETDATE())");