Skip to content

Logging your Sql with LINQ Update Commands. Simple Logging to your Visual Studio 2008 Debugger Output Console

Updated: at 02:12 PM

So, you want to do an update but are wondering what the hec LINQ is doing.  Turns out it is really easy.  All you have to do is run in the debugger and add the Log option to your data context.

Here is an example:

DataClassesGeneralDataContext db3pLogicContext;
db3pLogicContext.Log = Console.Out;
 
var companyQuery = from tbl in db3pLogicContext.Companies
                   where tbl.ParentId != 0
                   select tbl;
 
int totalCntParents = companyQuery.Count();
foreach (DBAccess.Company co in companyQuery)
{
    co.CreateDate = DateTime.Now;
    break;
}
 
db3pLogicContext.SubmitChanges();
 

Now, when you run the code, you can look on your debugger output and see something that looks like this:

image
 

Also, if you want to actually execute select type statements, take a look at the Query Visualizer posted on Scott Guthrie's web site:

http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects