您如何查看在LINQ to SQL中的SubmitChanges上导致错误的sql? [英] How can you see the sql that is causing an error on SubmitChanges in LINQ to SQL?

查看:74
本文介绍了您如何查看在LINQ to SQL中的SubmitChanges上导致错误的sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些LINQ to SQL,有时会抛出

I've got some LINQ to SQL that sometimes throws a

无法在具有唯一索引的对象'dbo.Table'中插入重复的键行 'IX_Indexname'.该语句已终止."

"Cannot insert duplicate key row in object 'dbo.Table' with unique index 'IX_Indexname'.The statement has been terminated."

是否有某种方法可以打开日志记录或至少调试到数据上下文中,以查看在引发错误时正在执行什么sql?

Is there some way I can turn on logging or at least debug into the datacontext to see what sql is being executed at the time that error is raised?

更新:我应该提到我对GetChangeSet()方法有所了解,我想知道在DataContext上是否有一个属性,该属性可以显示最近执行的SQL,还是在该属性上?显示SQL的sql异常.

Update: I should have mentioned I know about the GetChangeSet() method, I was wondering if there is a property on the DataContext that shows the last SQL that was executed, or a property on the sql exception that shows the SQL.

有关此错误的奇怪的事情是,在变更集中,只有一个更新&唯一要更改的字段是日期时间字段,该日期时间字段不在导致错误的索引中.

The odd thing about this error is that in the change sets, there is only one update & the only field that's changing is a datetime field that isn't in the index that causing the error.

推荐答案

一种简单的方法是使用

A simple way to do this is to use the DataContext.Log property:

using (MyDataContext ctx = new MyDataContext())
{
    StringWriter sw = new StringWriter();
    ctx.Log = sw;

    // execute some LINQ to SQL operations...

    string sql = sw.ToString();
    // put a breakpoint here, log it to a file, etc...
}   

这篇关于您如何查看在LINQ to SQL中的SubmitChanges上导致错误的sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆