如何从 LINQ DataContext.SubmitChanges() 获取 TSQL 查询
我正在使用 Linq to SQL。 我有一个 DataContext,我正在对其进行 .SubmitChanges()'ing。 插入身份字段时出错,我想查看它用于插入此身份字段的查询。
我在快速观看中没有看到查询本身; 我可以从调试器中的哪里找到它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
许多人一直在编写自己的“DebugWriter”并像这样附加它:
这会将 Linq-to-Sql 正在执行的所有操作输出到 Visual Studio 的调试窗口中。
Lots of people have been writing their own "DebugWriter" and attaching it like so:
This will output everything that Linq-to-Sql is doing into Visual Studio's debug window.
进一步波特曼的回答 ,如果您是控制台应用程序,则很简单:
或者您可以使用 Linq2SQL Profiler 之类的东西,这是一个相当出色的工具,实际上是完成这项工作的正确工具:
Further to Portman's answer, if you're a console application it's as simple as:
Or you could use something like Linq2SQL Profiler which is a rather excellent tool and in fact the right tool for the job:
实际上你的问题有一个非常简单的答案
只需将其粘贴到你的手表窗口中
There is actually a very simple answer to your question
Just paste this in your watch window
运行 SQL Profiler(如果有)。 它将显示数据库的所有流量,包括 SQL 命令文本。
Run SQL Profiler if you have it. It'll show all traffic to your database, including SQL command text.
我同意 Linq to SQL Profiler 是完成这项工作的正确工具。 但如果您不想花钱或者只需要做一些简单的事情,我喜欢 DebugTextWriter 方法。
读完这个问题后,我开始寻找更强大的东西。 事实证明,Damien Guard 还 写了一篇非常好的文章,介绍如何构建不同的编写器来处理不同的事情,例如输出到内存、调试、文件、多个目标,甚至使用简单的委托。
我最终使用了他的一些想法并编写了一个可以处理多个委托的 ActionTextWriter,我想我应该在这里分享它:
您可以添加任意数量的操作。 此示例通过 Debug.Write 写入 Visual Studio 中的日志文件和控制台:
当然,如果您想制作更简单的文件以便即兴使用,您可以随时扩展 ActionTextWriter...编写通用方法和重用,对吗?
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:
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:
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?
以下是详细说明: http://debugmode.net/2011 /06/26/logging-in-linq-to-sql/
Here is detailed description: http://debugmode.net/2011/06/26/logging-in-linq-to-sql/