how to see generated sql from a linq query

后端 未结 7 1386
忘掉有多难
忘掉有多难 2020-12-10 01:22

Just trying to get the sql that is generated by a linq query.

相关标签:
7条回答
  • 2020-12-10 01:36

    The easiest way i could suggest is to go with Database Log. Put Logafter initializing DataContext and you will be able to track whatever is done by EF on visual studio output window.

      DataContext db = new DataContext();
      db.Database.Log = generatedSQL =>
       {
          Debug.WriteLine(generatedSQL);
       };
    
    0 讨论(0)
  • 2020-12-10 01:38

    Use SQL Profiler if you are using SQL Server as your database.

    0 讨论(0)
  • 2020-12-10 01:46

    There are 3 ways do that.

    1.You can use LINQPad.It's Free http://www.linqpad.net/

    2.You can use SQL Server Profiler inside the Sql Server (Tools --> SQL Server Profiler)

    3.You can use Visual Studio Debugger for Generate T-Sql.(with any visual studio version)

    0 讨论(0)
  • 2020-12-10 01:48

    Another way

    From the MSDN article How to: Display Generated SQL (LINQ to SQL)

    Set the DataContext.Log Property to Console.Out and you'll see it in the console

    0 讨论(0)
  • 2020-12-10 01:51

    With Linq2Sql

    dc.GetCommand(query).CommandText
    

    see http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getcommand.aspx for more info.

    But I usually use LinqPad

    0 讨论(0)
  • 2020-12-10 01:58

    This popped up on Google, it's an 8-part tutorial. I think it will keep you busy for a few hours, it seems quite detailed to me.

    1: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

    2: http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx

    3: http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx

    4: http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx

    5: http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx

    6: http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

    7: http://weblogs.asp.net/scottgu/archive/2007/08/23/linq-to-sql-part-7-updating-our-database-using-stored-procedures.aspx

    8: http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx

    Good luck.

    0 讨论(0)
提交回复
热议问题