Check LINQ query against SQL Server database

前端 未结 5 1707
天涯浪人
天涯浪人 2021-02-06 11:25

Is there any way to in .NET to see what LINQ query against database we are firing? For eg. I am wring a query in LINQ and I want to see that what SQL query is firing to communic

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 11:53

    If you have a DbContext on which you firing your LINQ queries you can simply set the DbContext.Database.Log property to something like this:

    yourContext.Database.Log = (msg => System.Diagnostics.Debug.Write(msg, "SQL"));
    

    After this, every SQL query shows up in the Debug console from your Visual Studio with the category SQL.

提交回复
热议问题