How to get the TSQL Query from LINQ DataContext.SubmitChanges()

后端 未结 7 919
猫巷女王i
猫巷女王i 2020-12-13 20:48

I\'m using Linq to SQL. I have a DataContext against which I am .SubmitChanges()\'ing. There is an error inserting the identity field, and I\'d like to see the query it\'s u

7条回答
  •  臣服心动
    2020-12-13 21:29

    Lots of people have been writing their own "DebugWriter" and attaching it like so:

    // Add this class somewhere in your project...
    class DebugTextWriter : System.IO.TextWriter {
       public override void Write(char[] buffer, int index, int count) {
           System.Diagnostics.Debug.Write(new String(buffer, index, count));
       }
    
       public override void Write(string value) {
           System.Diagnostics.Debug.Write(value);
       }
    
       public override Encoding Encoding {
           get { return System.Text.Encoding.Default; }
       }
    }
    
    // Then attach it to the Log property of your DataContext...
    myDataContext.Log = new DebugTextWriter()
    

    This will output everything that Linq-to-Sql is doing into Visual Studio's debug window.

提交回复
热议问题