title: Why should I use GetOriginalEntityState() in my LINQ To SQL repository save method?
body: <p>I'm looking at an example of a save method in a Products repository from Steven Sanderson's book, Pro ASP.NET MVC 2 Framework:</p>
  
  <pre><code>public void SaveProduct(Product product)
  {
      // if new product, attach to DataContext:
      if (product.ProductID == 0)
          productsTable.InsertOnSubmit(product);
      else if (productsTable.GetOriginalEntityState(product) == null)
      { 
          // we're updating existing product
          productsTable.Attach(product);
          productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
      }
      productsTable.Context.SubmitChanges();            
  }
  </code></pre>
  
  <p>I do not understand the logic in the <code>else if</code> line:</p>
  
  <pre><code>else if (productsTable.GetOriginalEntityState(product) == null)
  </code></pre>
  
  <p>As I understand it,  <code>GetOriginalEntityState()</code> returns the original state of the specified entity.. in this case that entity is <code>product</code>.</p>
  
  <p>So this else if statement reads to me like: <em>"If an original doesn't exist then..."</em> but that doesn't make sense because the book is saying that this checks that we are modifying a record that already DOES exist.</p>
  
  <p>How should I understand <code>GetOriginalEntityState</code> in this context?</p>
  
  <h3>Edit</h3>
  
  <p>By the way, this excerpt came from chapter 6, page 191... just in case anyone has the book and wants to look it up. The book just has that function in the code sample but it never explains what the function does.</p>