title: Using a GUID as the ID in a database with ASP.NET MVC
body: <p>I am learning ASP.NET MVC. I am following one of the basic tutorials on <a href="http://www.asp.net/mvc/whatisaspmvc/" rel="noreferrer">asp.net</a>. Since I do not always follow the tutorials to the letter, I decided to use a GUID as the identity column instead of an integer. Everything was working fine until I got up to the point of adding a new record to the database through the MVC application. When I added the new record, it inserted a blank GUID instead of a generated one. Here is the segment of code-behind that handles the insertion:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Create([Bind(Exclude = "id")]Movie movieToCreate)
  {
      try
      {
          _entities.AddToMovieSet(movieToCreate);
          _entities.SaveChanges();
  
          return RedirectToAction("Index");
      }
      catch
      {
          return View();
      }
  }
  </code></pre>
  
  <p>The <code>[Bind(Exclude = "id")]</code> line 'ignores' the ID column, since it is autogenerated. In the tutorial, the ID is auto-incremented but I think that is because it is an integer. I tried adding a line to this method:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Create([Bind(Exclude = "id")]Movie movieToCreate)
  {
      try
      {
          movieToCreate.id = Guid.NewGuid();
          _entities.AddToMovieSet(movieToCreate);
          _entities.SaveChanges();
  
          return RedirectToAction("Index");
      }
      catch
      {
          return View();
      }
  }
  </code></pre>
  
  <p>But the id is still an empty GUID. Can anyone provide me with some information on why this is and perhaps how to resolve it?</p>