Adding SQL comment to Linq generated query so that

2019-05-07 11:55发布

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!

标签: c# linq logging
1条回答
太酷不给撩
2楼-- · 2019-05-07 12:17

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();
}
查看更多
登录 后发表回答