Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging LINQ to SQL SubmitChanges()

I am having a really hard time attempting to debug LINQ to SQL and submitting changes.

I have been using http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx, which works great for debugging simple queries.

I'm working in the DataContext Class for my project with the following snippet from my application:

JobMaster newJobToCreate = new JobMaster();
newJobToCreate.JobID = 9999
newJobToCreate.ProjectID = "New Project";
this.UpdateJobMaster(newJobToCreate);
this.SubmitChanges();

I will catch some very odd exceptions when I run this.SubmitChanges;

Index was outside the bounds of the array.

The stack trace goes places I cannot step into:

at System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
   at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
   at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
   at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
   at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges()
   at JobTrakDataContext.CreateNewJob(NewJob job, String userName) in D:\JobTrakDataContext.cs:line 1119

Does anyone have any tools or techniques they use? Am I missing something simple?

EDIT: I've setup .net debugging using Slace's suggestion, however the .net 3.5 code is not yet available: http://referencesource.microsoft.com/netframework.aspx

EDIT2: I've changed to InsertOnSubmit as per sirrocco's suggestion, still getting the same error.

EDIT3: I've implemented Sam's suggestions trying to log the SQL generated and to catch the ChangeExceptoinException. These suggestions do not shed any more light, I'm never actually getting to generate SQL when my exception is being thrown.

EDIT4: I found an answer that works for me below. Its just a theory but it has fixed my current issue.

like image 692
ben Avatar asked Sep 17 '08 19:09

ben


2 Answers

I always found useful to know exactly what changes are being sent to the DataContext in the SubmitChanges() method.

I use the DataContext.GetChangeSet() method, it returns a ChangeSet object instance that holds 3 read-only IList's of objects which have either been added, modified, or removed.

You can place a breakpoint just before the SubmitChanges method call, and add a Watch (or Quick Watch) containing:

ctx.GetChangeSet();

Where ctx is the current instance of your DataContext, and then you'll be able to track all the changes that will be effective on the SubmitChanges call.

like image 195
Christian C. Salvadó Avatar answered Nov 08 '22 21:11

Christian C. Salvadó


First, thanks everyone for the help, I finally found it.

The solution was to drop the .dbml file from the project, add a blank .dbml file and repopulate it with the tables needed for my project from the 'Server Explorer'.

I noticed a couple of things while I was doing this:

  • There are a few tables in the system named with two words and a space in between the words, i.e. 'Job Master'. When I was pulling that table back into the .dbml file it would create a table called 'Job_Master', it would replace the space with an underscore.
  • In the orginal .dbml file one of my developers had gone through the .dbml file and removed all of the underscores, thus 'Job_Master' would become 'JobMaster' in the .dbml file. In code we could then refer to the table in a more, for us, standard naming convention.
  • My theory is that somewhere, the translation from 'JobMaster' to 'Job Master' while was lost while doing the projection, and I kept coming up with the array out of bounds error.

It is only a theory. If someone can better explain it I would love to have a concrete answer here.

like image 8
ben Avatar answered Nov 08 '22 21:11

ben