tags:

views:

108

answers:

2

How would one go about seeing the SQL generated by a LINQ2SQL insert? Let's say that I have

        db.Elephants.InsertOnSubmit(elephantInstance);
        db.SubmitChanges();

is there anyway to see the SQL? I've installed the LINQtoSQL visualizer Scott Gu mentions (http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx) and it does seem to work for full LINQ queries but not for inserts such as this.

+1  A: 

The SQL Server Profiler will show you the generated queries. It will also show you the execution plans that the SQL Server uses to solve those queries, and provide you with ways to improve performance.

More info at http://msdn.microsoft.com/en-us/library/ms187929.aspx

Robert Harvey
+1  A: 

You can use DataContext.Log to log the SQL. You can set any TextWriter to the DataContext.Log property. If you want to log output to your debugger or console, try this http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11 - and you're good to go.

Richard Hein