Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding SQL comment to Linq generated query so that it is visible in SQL profiler

Tags:

c#

logging

linq

We want to use Linq to SQL for a project. That's the first time we use Linq. Typically we use just stored procedure calls.

So far everything is working great, but the DBA's are asking us whether we can mark the Linq generated SQL queries in a way that is visible in Profiler.

I googled and searched Stackoverflow and I found various ways to log the generated SQL. But that's not exactly what I want. I think ideal would be if I could stick a SQL comment into the generated SQL. Would that be visible in Profiler?

Thanks for any ideas!

like image 692
StefanS Avatar asked Jan 20 '23 19:01

StefanS


1 Answers

You could use a unique connection string that includes a specific "Application Name" to identify LINQ to SQL queries.

alt text

alt text

Here is an example of how you can set the Application Name in code:

string connectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
builder.ApplicationName = "linqtosql";

using (var context = new DataContext(builder.ConnectionString)) {
    var list = context.Customers.ToList();
}
like image 174
Tom Brothers Avatar answered Jan 30 '23 21:01

Tom Brothers