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

后端 未结 7 898
猫巷女王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:26

    I agree that Linq to SQL Profiler is the right tool for this job. But if you don't want to spend the money or just need to do something simple, I like the DebugTextWriter approach.

    After reading this question I went off looking for something more robust. It turns out Damien Guard also wrote a very nice article about building different writers to deal with different things like outputting to Memory, Debug, a File, Multiple Destinations, or even using simple Delegates.

    I wound up using a couple of his ideas and writing an ActionTextWriter that can handle more than one delegate, and I thought I would share it here:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    namespace Writers
    {
        public class ActionTextWriter : TextWriter
        {
            protected readonly List> Actions = new List>();
    
            public ActionTextWriter(Action action)
            {
                Actions.Add(action);
            }
    
            public ActionTextWriter(IEnumerable> actions)
            {
                Actions.AddRange(actions);
            }
    
            public ActionTextWriter(params Action[] actions)
            {
                Actions.AddRange(actions);
            }
    
            public override Encoding Encoding
            {
                get { return Encoding.Default; }
            }
    
            public override void Write(char[] buffer, int index, int count)
            {
                Write(new string(buffer, index, count));
            }
    
            public override void Write(char value)
            {
                Write(value.ToString());
            }
    
            public override void Write(string value)
            {
                if (value == null)
                {
                    return;
                }
    
                foreach (var action in Actions)
                {
                    action.Invoke(value);
                }
            }
        }
    }
    

    You can add as many actions as you like. This example writes to a log file and the Console in Visual Studio via Debug.Write:

    // Create data context
    var fooDc = new FooDataContext();
    
    // Create writer for log file.
    var sw = new StreamWriter(@"C:\DataContext.log") {AutoFlush = true};
    
    // Create write actions.
    Action writeToDebug = s => Debug.Write(s);
    Action writeToLog = s => sw.Write(s);
    
    // Wire up log writers.
    fooDc.Log = new ActionTextWriter(writeToDebug, writeToLog);
    

    And of course if you want to make simpler ones to use off the cuff, you can always extend ActionTextWriter... write the generic approach and reuse, right?

    using System.Diagnostics;
    using System.IO;
    
    namespace Writers
    {
        public class TraceTextWriter : ActionTextWriter
        {
            public TraceTextWriter()
            {
                Actions.Add(s => Trace.Write(s));
            }
        }
    
        public class FileTextWriter : ActionTextWriter
        {
            public FileTextWriter(string path, bool append = false)
            {
                var sw = new StreamWriter(path, append) {AutoFlush = true};
                Actions.Add(sw.Write);
            }
        }
    }
    

提交回复
热议问题