none
Intermittent missing insertion in DataContext RRS feed

  • Question


  • Hi all

    I have problem where Linq is inserting the correct data into the DB, but the DataContext doesn't always have the correct data (but it does sometimes). The following pseudocode hopefully illustrates the code I'm using.

    Code Snippet

    public class A;  // Linq to SQL class for table A
    public class B;  // Linq to SQL class for table B
    public class AB; // Linq to SQL class for M:N relationship table between A and B

    var ab = new AB

    {

       ab.a = new A {...};

       ab.b = new B {...};

    };

    context.InsertOnSubmit(ab);

    context.SumbitChanges();


    // later on ...

    if (some condition)

    {

    // create a new B object

    var b2 = new B {...};

    // insert a new relationship
    AB.InsertOnSubmit(new AB
    {
        a = ab.a,
        b = b2      
    };

    // delete the old one.

         AB.DeleteOnSubmit(ab);

    }


    // later still in a test method after the above code has run...

    context.SubmitChanges();

    Assert.That(a.ABs.Count > 0);  // this sometimes fails!




    In the database, this works fine. The orginal AB relationship is deleted, and a new one inserted.

    However, some of the time, the Assert fails (even though the correct thing has been done in the DB).

    Basically, it looks sometimes like the first AB instance is deleted, but the second AB instance in not correctly inserted (as far as the current data context is concerned anyway).

    This seems to depend on the order in which things happen (I am dealing with multiple AB relationships). However, the code is identical for every case, and shouldn't be dependant on the order of the relationships.

    Any ideas as to why this might be happending?

    Thanks

    --
    Simon Davy

    Wednesday, March 12, 2008 6:23 PM

Answers

  • Without more specifics about what happens in your ommitted code, the suggestion would be to assert against a new context or perhaps to first refresh the relationship with context.Refresh(Refreshmode, a);

     

    However, a more complete reproduction of the problem would be helpful in diagnosing what exactly is happening.

     

     

    - Josh

    Saturday, May 10, 2008 1:06 AM