调试Linq to sql代码是, 如果遇到错误,很难判断错误的原因是什么,如果能够输出实际执行的sql原文,对于我们寻找错误的原因有有很大帮助。
以下是我用到的方法:
StringBuilder sql =
new StringBuilder();
try
{
using ( var context = new DataContext())
{
context.Log = new System.IO.StringWriter(sql);
context.SubmitChanges();
}
}
finally
{
sql.ToString();
}
try
{
using ( var context = new DataContext())
{
context.Log = new System.IO.StringWriter(sql);
context.SubmitChanges();
}
}
finally
{
sql.ToString();
}