How to track database conversions

I have a web application that uses the Entity Framework to query a SQL Server database. I would like to know programmatically how many round-trip requests were made in the database. The idea is to log this information in order to easily detect errors in which the relationship was not included and caused many rounds.

Is there any way to achieve this? I don't mind if the solution is specific to SQL Server.

Note. I want to track the database programmatically , so tools like SQL Server Profiler do not suit me. I want to know, at the end of the request and in the code that processes the request, how many requests were made by this request.

+3
source share
5 answers

You can use the statistics function of the SQL connection provider .

+1
source

This is my final decision based on the proposal of Craig Stutz.

using(var database = new MyEntities())
{
    SqlConnection sqlConnection = null;

    var entityConnection = _database.Connection as EntityConnection;
    if (entityConnection != null)
    {
        sqlConnection = entityConnection.StoreConnection as SqlConnection;

        // Enable statistics
        sqlConnection.StatisticsEnabled = true;
    }

    // Access the database

    if (sqlConnection != null)
    {
        var statistics = sqlConnection.RetrieveStatistics();
        var selectCount = (long)statistics["SelectCount"];

        // Do something with the statistics
    }
}
+2
source
+1

:

  • T4 , , , .
  • DLL- LinqPad, , , LinqPad SQL.
+1

I did this with difficulty using the SQL Server 2008 Profiler, if you set the correct filters in the profiler, you will only see calls made by EF.

0
source

Source: https://habr.com/ru/post/1775239/


All Articles