tsi*_*ilb 15 c# linq datacontext submitchanges linq-to-sql
我正在使用Linq to SQL.我有一个DataContext,我就是.SubmitChanges()'.插入标识字段时出错,我希望看到它用于插入此标识字段的查询.
我在快速监视器中看不到查询本身; 我在哪里可以从调试器中找到它?
Por*_*man 21
很多人一直在编写自己的"DebugWriter"并将其附加如下:
// 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()
Run Code Online (Sandbox Code Playgroud)
这将输出Linq-to-Sql所做的一切到Visual Studio的调试窗口.
Kev*_*Kev 11
继Portman的回答之后,如果你是一个控制台应用程序,它就像下面这样简单:
myDataContext.Log = Console.Out;
Run Code Online (Sandbox Code Playgroud)
或者你可以使用像Linq2SQL Profiler这样的东西,这是一个相当优秀的工具,实际上是工作的正确工具:
实际上你的问题答案非常简单
只需将其粘贴到您的观察窗口即可
((System.Data.Objects.ObjectQuery)myLinqQueryVar).ToTraceString()
Run Code Online (Sandbox Code Playgroud)