title: InvalidOperationException when calling SaveChanges in .NET Entity framework
body: <p>I'm trying to learn how to use the Entity framework but I've hit an issue I can't solve.
  What I'm doing is that I'm walking through a list of Movies that I have and inserts each one into a simple database.</p>
  
  <p>This is the code I'm using</p>
  
  <pre><code>private void AddMovies(DirectoryInfo dir)
  {
      MovieEntities db = new MovieEntities();
      foreach (DirectoryInfo d in dir.GetDirectories())
      {
          Movie m = new Movie { Name = d.Name, Path = dir.FullName };
          db.AddToMovies(movie);
      }
      db.SaveChanges();
  }
  </code></pre>
  
  <p>When I do this I get an exception at db.SaveChanges() that read.</p>
  
  <blockquote>
    <p>The changes to the database were
    committed successfully, but an error
    occurred while updating the object
    context. The ObjectContext might be in
    an inconsistent state. Inner exception
    message: AcceptChanges cannot continue
    because the object's key values
    conflict with another object in the
    ObjectStateManager. Make sure that the
    key values are unique before calling
    AcceptChanges.</p>
  </blockquote>
  
  <p>I haven't been able to find out what's causing this issue.
  My database table contains three columns<br>
  Id int autoincrement<br>
  Name nchar(255)<br>
  Path nchar(255)</p>
  
  <p>Update:
  I Checked my edmx file and the SSDL section have the StoreGeneratedPattern="Identity" as suggested. I also followed the blog post and tried to add ClientAutoGenerated="true" and StoreGenerated="true" in the CSDL as suggested there. This resulted in compile errors ( Error 5: The 'ClientAutoGenerated' attribute is not allowed.). Since the blog post is from 2006 and it has a link to a follow up post I assume it's been changed.</p>
  
  <p>However, I cannot read the followup post since it seems to require an msdn account.</p>