How to debug a linq to sql InsertOnSubmit statemen

2019-05-28 16:37发布

The following code seased to work.

db.DBUsers.InsertOnSubmit(new DBUser
    {
        AllTheStuff = valuesBeyondYourWildestDreams
    }
);
db.SubmitChanges();

My guess is something changed at the database and the submit is failing because the mapping is off.
As the linq visualiser isn't working for me (bonus points for fixing that) I want to find another way to know what exactly is going wrong and why the submit is failing silently.

Update
I tried using

db.SubmitChanges(ConflictMode.FailOnFirstConflict);

to get an exception, but it seems the submit works. Except that there is no actual new entity in the database.

4条回答
Melony?
2楼-- · 2019-05-28 17:27

For starters I would do:

db.Log = Console.Out;
查看更多
Deceive 欺骗
3楼-- · 2019-05-28 17:29

This works quite well at debugging linq to sql for us.

Its sends the log information to the output window.

http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11

查看更多
淡お忘
4楼-- · 2019-05-28 17:30

You could try using SQL Server Profiler if it's an option - start a new trace, connect to the relevant database, then select which events you want to capture.

For basic LINQ debugging, I've found that SP:StmtCompleted (under Stored Procedures) and SQL:StmtCompleted (under TSQL) are enough to show you what SQL LINQ is trying to run.

If you find you're picking up lots of unwanted commands, you can filter these out. For example, I filter out everything coming from the msdb and master databases.

查看更多
可以哭但决不认输i
5楼-- · 2019-05-28 17:35

You could also use LINQPad - copy your LINQ code into there, reference the database, and put dumps in to effectively debug the LINQ.

查看更多
登录 后发表回答