title: SaveChanges() on context having new/modified objects doesn't do anything
body: <p>I used to have a WORKING database for my ASP.NET site, up until the point that I've learned that using static Database Context objects throughout the app lifetime is a very bad practice.</p>
  
  <p>So, initially, I used to have this:</p>
  
  <pre><code>    public class Database
      {
          MainDBContainer db;
          static Database()
          {
              db = new MainDBContainer();
          }
      }
  
      internal static SiteUser GetUser(string username)
      {
           return db.SiteUserSet.SingleOrDefault(u =&gt; u.Username == username);
      }
  </code></pre>
  
  <p>And, as you may guess, I've always used this static object to access/modify the database. Well, because of what I've read everywhere (that the static db context is bad), I decided to change the code to this:</p>
  
  <pre><code>    internal static SiteUser GetUser(string username)
      {
          //notice no static constructor for class.
  
          using (MainDBContainer db = new MainDBContainer())
          {
              return db.SiteUserSet.SingleOrDefault(u =&gt; u.Username == username);
          }
      }
  </code></pre>
  
  <p>The problem with this code (not particularly "this" very code, but the codes that modify something. by meaning this, I meant that I wrapped every db access with the using keyword just like this one), <code>when db.SaveChanges()</code> is called, it doesn't throw any exception, but nothing happens too. Within the same context, everything works, but that's it. <strong>NOTHING GETS WRITTEN TO THE ACTUAL SQL DATABASE.</strong> Before, I had everything set up and working and as soon as <code>SaveChanges()</code> was called, everything was immediately flushed to the database. My user authentication is even broken (straightforward: client sends username/passwordhash, and if its right server sends a random token, and client uses token with every request): User calls login method and successfully obtains a token, but whenever any other code interacts with the User object, from another context, the token is whatever the DB has before, and <code>SaveChanges</code> in the login method had no effect. It's the same for all methods, I just gave the Login one as a simple example. Am I missing something? Did I miss something obvious while porting code? Probably yes, but what's it?</p>
  
  <p>Thanks,
  Can.</p>