title: Code first - Cannot insert the value NULL into column 'Id'
body: <p>This is my code, very simple... </p>
  
  <pre><code> var newUser = new User();
          newUser.Id=id;
          newUser.Email = email;
         this.DataContext.Set&lt;User&gt;().Add(newUser);
          this.DataContext.SaveChanges();
  </code></pre>
  
  <p>The error I get is a sqlexception at <code>this.DataContext.SaveChanges();</code> stating that: </p>
  
  <blockquote>
    <p>Cannot insert the value NULL into column 'Id', table
    'xxxxx.dbo.Users'; column does not allow nulls. INSERT fails.</p>
  </blockquote>
  
  <p>I have debugged and found that there is value in Id &amp; Email in newUser at 
  <code>this.DataContext.Set&lt;User&gt;().Add(newUser);</code></p>
  
  <p>If this is the case, how is the value becoming null?</p>
  
  <p>The error stack trace:</p>
  
  <pre><code>[DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
     System.Data.Entity.Internal.InternalContext.SaveChanges() +204
     System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +23
     System.Data.Entity.DbContext.SaveChanges() +20
  </code></pre>
  
  <p>I have not been able to understand or solve this....</p>
  
  <p>Would sincerely appreciate any help in this...</p>
  
  <p>Regards
  Arnab</p>
  
  <p><strong>Solution</strong> </p>
  
  <p>Ok, thanks to Ladislav to point me in the right direction: 
  Adding the attribute <code>[DatabaseGenerated(DatabaseGeneratedOption.None)]</code> solved the problem. </p>