title: Inserting item in LINQ to SQL datacontext - doesn't appear until application restart
body: <p>I have an ASP.NET MVC (using the release candidate) application that works with a class library that among other things uses LINQ to SQL for data persistance.</p>
  
  <p>One of the thing the app/db maintains, is the concept of a "Folder" - like a disk folder, but with a hierarchy that only exists in the database. Other database objects live in these folders, seen from the perspective of the user.</p>
  
  <p>Each class in the class library has its own static reference to my DataContext object. In addition, the MVC controller each has it's own DataContext object.</p>
  
  <p>I have two Actions returning JSON data. One is a "GetFoldersJSON" which returns the folder structure in a format suitable for a select (dropdown) list. The other one is "AddFolderJSON" which accepts some form data, inserts a new Folder into the database, and then returns GetFoldersJSON() for sending the new folder list to the client. The client uses AJAX with these actions to refresh the dropdownlists once a new folder is created.</p>
  
  <p>The flow of this operation is as such:</p>
  
  <ol>
  <li><p>AddFolderJSON() action - Get the form information with new folder name, parent folder etc.</p></li>
  <li><p>Create new Folder object. 
  Perform a dataContext.Folders.InsertOnSubmit(newFolder);
  Perform a dataContext.SubmitChanges();
  Perform a dataContext.Refresh(OverwriteThingy, dataContext.Folders);
  return result of GetFoldersJSON()</p></li>
  <li><p>GetFoldersJSON() uses a function which resides in the Folder definition - a partial class extending the OR-mapped folder object. This function recursively adds folders and subfolders to a flat list using code like this:</p>
  
  <pre><code>var rootFolders = from f in db.Folders
                    where f.ParentFolder == null
                    orderby f.name
                    select f;
  </code></pre></li>
  </ol>
  
  <p>db is of course the local, static DataContext reference.</p>
  
  <p>Then a simple loop:</p>
  
  <pre><code>    foreach (var fldr in rootFolders)
      {
          AddFolderContents(list, fldr);
      }
  </code></pre>
  
  <p>The AddFolderContents() function then adds the current folder to the list, and proceeds to call itself for each subfolder of the current folder, thus creating a hierarchy. </p>
  
  <p>This works fine for data already in the database, but when we run this procedure after creating a new Folder, the new folder does not show up. If we refresh the page, it still doesn't show up. It doesn't actually show up until the application is restarted.</p>
  
  <p>I have tried to do Refresh() calls just about everywhere, and it seems to have no effect in this case.</p>
  
  <p>Is there a way to tell LINQ to SQL "Hey, I know what you're thinking, but DROP IT ALL, and just get ALL the data from the database, NOW!"?</p>
  
  <p>I have a nagging feeling I've heard about this before, but couldn't find it described here.</p>