none
Beginners question - Where is the new record? RRS feed

  • Question

  • I'm going through a basic LinqToSQL exercise in the 516 manual. 

    Given a DataContext generated from the standard NorthWind database, I've created a new order record.  I've done a InsertOnSubmit call to the Orders table.  Now, I've added a break point at the ctx.SubmitChanges() call. At that point I'm doing a QuickWatch to examine the ctx DataContext object.

    My question is this: Where is the new record?  Can it be viewed through the QuickWatch?

    Saturday, August 11, 2012 7:03 PM

Answers

  • Hi B. Chernick;

    To your question, "So, in between the InsertOnSubmit and the SubmitChanges, where is the new record?", It is in the DataContext internal list and a reference to that object can be gotten before you call SubmitChanges as follows :

    ACallToYourFunctionThatAddsOrder(); // Get a list of objects that have been inserted but not yet updated var entitiesToInsert = ctx.GetChangeSet().Inserts; // Create a List to hold a reference to the Inserted object. List<Order> newOrders = new List<Order>(); // Go through the list to find all objects of type Order foreach (var o in entitiesToInsert) { // Find the objects of type Order if( o.GetType().Name == "Order") { // Cast the Object to a Order type and add to list newOrders.Add((Order)o); } }

    // At this point you have a reference to Orders and the value of OrderId will be 0
    // but after you execute SubmitChanges OrderId will have the DB values assigned.


      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, August 11, 2012 10:58 PM

All replies

  • Hi B. Chernick;

    To your question, "Where is the new record?  Can it be viewed through the QuickWatch?", When you added the new Order record to the DataContext by executing the statement InsertOnSubmit you created a local variable/copy that will be sent to SQL server which will be added to the Order's table. When working with Linq To SQL or Linq To Entity Framework you can only see and work with the local copy of what is in the database. If you want to see the record keep a reference to the new record so that you may view it. Once you submit the changes the Primary key will be updated in the local copy to match the one in the database. 

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, August 11, 2012 7:25 PM
  • I'm afraid I find that bit confusing.   I wonder if I understand the concept of 'DataContext' correctly. 

    You're saying that even though I did add the new record object to a table within the DataContext object I still can't actually view it util it's actually added to the database itself?

    Saturday, August 11, 2012 7:37 PM
  • Hi B. Chernick;

    To  your question, "You're saying that even though I did add the new record object to a table within the DataContext object I still can't actually view it util it's actually added to the database itself?", No that is NOT what I am saying. The DataContext maintains the local values of the database data you retrieved or are about to insert, update or delete. Therefore they can always be viewed. To view them you will need to get a reference to the values you want to view. so if you made a query to the database and it returned 10 records the best way to get a reference to it is when you make the query assign its values to a list by using the ToList method of Linq. If you create a new record then assign it to a variable before using the InsertOnSubmit method. If the primary key is database generated then the only way you can see it on a new record is first update the database and by doing that Linq to SQL will update the local copy of the newly added record.

      

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, August 11, 2012 8:15 PM
  • Sorry.  Let me rephrase my question.

    In the example I'm using, I have this code in a popup WPF window:

        Order order = new Order
        {
              CustomerID = this.CustomerID,
              OrderDate = dtOrder.SelectedDate,
               RequiredDate = dtRequired.SelectedDate
         };
         ctx.Orders.InsertOnSubmit(order);

    The order variable is a local variable in a method.  (And then window closes anyway.)

    Only then do I call ctx.SubmitChanges().

    So, in between the InsertOnSubmit and the SubmitChanges, where is the new record?

    Saturday, August 11, 2012 8:35 PM
  • Hi B. Chernick;

    To your question, "So, in between the InsertOnSubmit and the SubmitChanges, where is the new record?", It is in the DataContext internal list and a reference to that object can be gotten before you call SubmitChanges as follows :

    ACallToYourFunctionThatAddsOrder(); // Get a list of objects that have been inserted but not yet updated var entitiesToInsert = ctx.GetChangeSet().Inserts; // Create a List to hold a reference to the Inserted object. List<Order> newOrders = new List<Order>(); // Go through the list to find all objects of type Order foreach (var o in entitiesToInsert) { // Find the objects of type Order if( o.GetType().Name == "Order") { // Cast the Object to a Order type and add to list newOrders.Add((Order)o); } }

    // At this point you have a reference to Orders and the value of OrderId will be 0
    // but after you execute SubmitChanges OrderId will have the DB values assigned.


      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, August 11, 2012 10:58 PM