如何分析 LINQ 查询
我发誓我以前见过如何做到这一点,但现在我真的需要这样做,我不记得我在哪里看到过它。我需要两个不同的东西 -
1)查看由 LINQ 查询生成的实际 SQL 查询,以及 2) 当 SQL 查询实际访问数据库执行任何操作(CRUD 操作)
是否有工具可以让我执行此操作?
编辑:
抱歉,应该提供更多详细信息。 -- 我正在使用 LINQ to Entities。 -- 此外,我没有 SQL Server 实例的管理员权限,因此无法使用 SQL Profiler。我总是可以打电话给 DBA 让他们帮我做,但这很麻烦。我应该提到这一点并且我道歉。我真正想要的是一个可以在我自己的机器上使用的工具,它允许我在处于调试模式(调试和单步执行代码)时查看 LINQ 查询何时命中数据库。
I swear I've seen how to do this before, but now that I actually need to do it I can't remember where I saw it at. I need two different things --
1) to see the actual SQL query that is generated by a LINQ query and
2) when the SQL query actually hits the database to do whatever (CRUD operations)
Is there a tool that will allow me to do this?
EDIT:
Sorry, should have given more detail.
-- LINQ to Entities is what I'm using.
-- Also, I don't have admin rights on our instance of SQL Server, so I can't use SQL Profiler. I could always call the DBA and have them do it for me, but it's a hassle. I should have mentioned that and I apologize. What I really want is a tool that I can use on my own box that will allow me to see when a LINQ query hits the database while I am in debug mode (debugging and stepping through the code).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试使用 SQL 事件探查器。很高兴看到 LINQ to SQL 正在生成什么。
LINQPad 也是编写 linq 和 sql 语句进行测试的绝佳工具。
Try using SQL Profiler. It's great for seeing what LINQ to SQL is generating.
LINQPad is also a great tool for writing linq and sql statements for testing.
LINQ to SQL 的
IQueryable
上的.ToString()
将向您显示查询。The
.ToString()
on theIQueryable
for LINQ to SQL will show you the query.有一些工具可以提供帮助。
L2SProf (Linq2Sql Profiler) 是一个付费工具,可以做很多事情来帮助您查看和优化查询。 http://l2sprof.com/
LinqPad 是一个很棒的工具,用于编写 linq to sql 查询并查看生成的 sql 。它还允许您编写 C# 代码来进行实验。这是一个很棒的编码便笺簿。有一个免费版本,但如果付费,您可以获得额外的功能。 http://www.linqpad.net/
否则,这里有免费查看生成的 sql 的说明内置内容: http://msdn.microsoft.com/en-us/library/ bb386961.aspx
There are a few tools that can help.
L2SProf (Linq2Sql Profiler) is a for pay tool that can do a lot to help you see and optomize your queries. http://l2sprof.com/
LinqPad is a great tool for write linq to sql queries and seeing the sql that comes out. It also lets you write c# code just to experiment with things. It's a great coding scratch pad. There is a free version, but you get extra features if you pay. http://www.linqpad.net/
Otherwise, there are instructions here for viewing the generated sql for free with builtin stuff: http://msdn.microsoft.com/en-us/library/bb386961.aspx
使用 Visual Studio 2010,打开 IntelliTrace,您将看到每个请求适用于 LINQ To SQL、LINQ To Entities 以及最终使用 ADO.NET 的所有内容。
Using Visual Studio 2010, turn on IntelliTrace and you'll see every request for LINQ To SQL, LINQ To Entities and generally everything that ultimately uses ADO.NET.
您是否正在寻找
DataContext.Log
属性? (我假设您使用的是 LINQ to SQL。)您可以创建一个
TextWriter
,每次写入时都会转储堆栈跟踪和时间戳,这将为您提供计时信息。Are you looking for the
DataContext.Log
property? (I'm assuming you're using LINQ to SQL.)You could create a
TextWriter
which dumped a stack trace and timestamp every time it was written to, which would give you the timing information.Log
属性,您可以将TextWriter
等插入其中。请参阅 ORM 文档以了解更多信息。SaveChanges
方法时。这与 (1) 相关,记录器将为您提供信息。Log
property you can plug aTextWriter
or so into. Consult the docs for ORM to learn more.SaveChanges
method. This ties in with (1), the logger will provide you with the information.这是一个解决方案,它非常复杂:
Here is a solution, it is very complex: http://blogs.msdn.com/b/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspx