Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:20:08+00:00 2026-06-14T14:20:08+00:00

OK, so you think you’re a real debugger? Try this one: I’ve got a

  • 0

OK, so you think you’re a real debugger? Try this one:

I’ve got a Linq2Sql project where suddenly we’ve discovered that occasionally, seemingly randomly, we get double-inserts of the same row.

I have subclassed DataContext and overridden SubmitChanges. Inside there I have made a call to GetChangeSet(), both before and after the call to base.SubmitChanges(), and used a text file logger to record the objects in the Inserts collection. Plus I hang on to a reference to the inserted objects long enough to record their autonumbered ID.

When the double-insert happens, I see in the DB that instead of one row each being inserted into MyTableA and MyTableB, there are two each. SQL Profiler shows four insert statements, one after the other in rapid succession:

insert into MyTableA(...
insert into MyTableB(...
insert into MyTableA(...
insert into MyTableB(...

I check in the debug log, and there are only two objects in the Inserts collection: one of MyClassA and one of MyClassB. After the call to base.SubmitChanges(), the changeset is empty (as it should be). And the autonumber IDs show the larger value of the newly inserted rows.

Another piece of useful information: the bug never happens when stepping through in debug mode; only when you run without breakpoints. This makes me suspect it’s something to do with the speed of execution.

We have been using the same DataContext subclass for over a year now, and we’ve never seen this kind of behavior before in our product. It only happens with MyClassA and MyClassB.

To summarize:

  1. From the debug log, everything looks like it’s working correctly.
  2. On SQL Profiler, you can see that a double-insert is happening.
  3. This behavior happens frequently but unpredictably, only to the two classes mentioned, excepting that it never happens when stepping through code in debug mode.

EDIT – New information:
Inside my DataContext subclass, I have the following code:

try {
  base.SubmitChanges(ConflictMode.ContinueOnConflict);
} catch (ChangeConflictException) {
  // Automerge database values for members that client has not modified.
  foreach (ObjectChangeConflict occ in ChangeConflicts) {
    occ.Resolve(RefreshMode.KeepChanges);
  }
}
// Submit succeeds on second try.
base.SubmitChanges(ConflictMode.FailOnFirstConflict);

MyTableA and MyTableB both have a mandatory foreign key OtherTableID referencing OtherTable. The double insert happens when a ChangeConflictException happens during an update of the common parent table OtherTable.

We’re on the scent, now…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:20:09+00:00Added an answer on June 14, 2026 at 2:20 pm

    looks like it’s a BUG in Linq2Sql! Here’s a repeatable experiment for you:

    using (var db1 = new MyDataContext()) {
      var obj1 = db1.MyObjects.Single(x => x.ID == 1);
      obj1.Field1 = 123;
      obj1.RelatedThingies.Add(new RelatedThingy {
                               Field1 = 456,
                               Field2 = "def",
                               });
      using (var db2 = new MyDataContext()) {
        var obj2 = db2.MyObjects.Single(x => x.ID == 1);
        obj2.Field2 = "abc";
        db2.SubmitChanges();
      }
      try {
        db1.SubmitChanges(ConflictMode.ContinueOnConflict);
      } catch (ChangeConflictException) {
        foreach (ObjectChangeConflict occ in ChangeConflicts) {
          occ.Resolve(RefreshMode.KeepChanges);
        }
      }
      base.SubmitChanges(ConflictMode.FailOnFirstConflict);
    }
    

    Result: MyObject record with ID = 1 gets updated, Field1 value is 123 and Field2 value is “abc”. And there are two new, identical records inserted to RelatedThingy, with MyObjectID = 1, Field1 = 456 and Field2 = “def”.

    Explain THAT!

    UPDATE: After logging this on Microsoft Connect, the nice folks at MS asked me to put together a little demo project highlighting the bug. And wouldn’t you know it – I couldn’t reproduce it. It seems to be connected to some weird idiosyncrasy of my project. Don’t have time to investigate further, and I found a workaround, anyway…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I have a basic understanding of this, but am hoping that someone
Think that I have many activities,and all I want is this: I have a
I think that asking this might be kind of silly but I'm still wondering
Think this should be an easy one. I understand how to establish a connection,
i think this is an easy one for you: I have a Controller in
think this should be an easy one... I want to get the currently selected
think about this: StreamReader reader = null; try { reader = new StreamReader(_fileName); }
I think this is probably a simple one, but I am just trying to
I think this question is so simple, that I am at a loss for
Think about doing this: import matplotlib.pyplot as plt plt.plot(x_A,y_A,'g--') plt.plot(x_B,y_B,'r-o') plt.show() How would you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.