Home

SubmitChanges works only on one entity

edited August 6

Hi,
I'm stuck with an issue I'm getting with a linqPad 5 C# Program I'm writing.

I'm connecting to a Sql Server database with default LinqPad driver (Linq To SQL).

So, what I'm doing is something like this:

Main()
{
    DoWork(123);
}

public void DoWork(int id)
{
    var a = Transactions.SingleOrDefault(t => t.Id == id);
    ProcessTransaction(a);
}

public void ProcessTransaction(Transaction transaction)
{
    var b = Operations.Where(o => o.TransactionId == transaction.Id);

    //... do some validations

    var order = Orders.Single(o => o.Id == transaction.OrderId);
  
   foreach (var operation in operations)
   {
      //... do some validations
      //... pass order through some methods that modify a prop of one or more elements of the order.OrderLines nav prop using foreach 

       operation.Ok = true;
   }

   transaction.Completed = true;
   
   SubmitChanges();
}

What I'm getting from this is that only the transaction object change is committed to the database upon SubmitChanges.

By watching the order variable, I can see that the expected property in the order lines is correctly modified until the SubmitChanges call, but right after it gets called the property is reverted back to its original value.

Also, in the SQL pane, I can see the UPDATE query for the transaction object, but no update queries nor for the order object, nor for the operations.

Any suggestions?

The main difference I guess I see is that the transaction object never gets enumerated, while the other two do...

ps: Transaction is a regular database table entity, it has nothing to do with SQL transactions.

Thank you!
a.

Comments

  • I have tested your program and it works fine as long as 'operations' is derived from 'b' (i just did a .ToList()) and the assigned value (operation.Ok = true) is actually different than the current value in the database table (Operation.Ok = 0).

    http://share.linqpad.net/8h8xtd.linq

  • edited August 7

    Hi,
    thank you, but I'm still stuck on the "order" problem.
    Now, assume that at line 29 of your code you have something like this:

    var order = Orders.Single(o => o.Id == myId);
    

    Assume that at line 33 of your code, instead of the comment you have a call like this:

    this.orderModifier.ChangeOrder(order);
    
    // ...where:
    
    public class OrderModifier 
    {
        public void ChangeOrder(Order order)
        {
            foreach (var line in order.Lines)
            {
                line.StateId = 2;
            }
    
            order.StateId = 2;
        }
    }
    

    Consider that both the StateId on the order and the orderline are foreign keys (with their own navigation property) to the same "Status" table, just a lookup.

    What I'm getting when the SubmitChanges method is called is that the Order table is updated, while the OrderLines don't (actually, in my case it's a 1 item collection).

    I have tried accessing the lines with linq, foreach, and for either - with and without ToList()... same result: every time my debugger watch says that the orderlines StateId property right before calling SubmitChanges is set to 2, and after it returns to its previous value.

    2 is a valid foreign key to the lookup table (the order gets updated).

    I have also tried to dump the changeset of the OrderLines and Order context before the SubmitChanges call, and both report a pending update!

    Oh, and of course, if I try in a separate C# Statements tab something like:

    var order = Orders.Single(o => o.Id == myId);
    order.OrderLines[0].StateId = 2;
    order.StateId = 2;
    submitChanges();
    

    .... it works.

    a.

  • Working example - http://share.linqpad.net/56s74n.linq

    I understand you are trying to update a column with a foreign key, if you look at the table tree:

    As long as you update the field (StateId) and not the Id of the navigation property (State.Id) you should be good.

  • I see... but unfortunately no, I'm not updating State.Id, I'm updating the StateId prop... as I wrote before, writing single statements in a separate Statements tab (more or less like you did) works...

Sign In or Register to comment.