title: Using NHibernate mapping by code: Cannot insert explicit value for identity column in table 'DietUser' when IDENTITY_INSERT is set to OFF
body: <p>Took me a while to find an answer for this so thought I'd share the love.</p>
  
  <hr>
  
  <p>When using NHibernate's new mapping by code with SQL Server I'm unable to save an entity. When saving an entity a System.Data.SqlClient.SqlException is thrown with the following message (minus the table name):</p>
  
  <blockquote>
    <p>"Cannot insert explicit value for identity column in table 'DietUser' when IDENTITY_INSERT is set to OFF."</p>
  </blockquote>
  
  <p>My table uses an identity ID and the entity &amp; mappings look like this:</p>
  
  <pre><code>public class User
  {
      public virtual int Id { get; set; }
      public virtual string Name { get; set; }
      public virtual string Username { get; set; }
      public virtual string Password { get; set; }
      public virtual short DailyPoints { get; set; }
  }
  
  public class UserMapping : ClassMapping&lt;User&gt;
  {
      public UserMapping()
      {
          Id(x =&gt; x.Id);
          Property(x =&gt; x.Name);
          Property(x =&gt; x.Username);
          Property(x =&gt; x.Password);
          Property(x =&gt; x.DailyPoints);
      }
  }
  </code></pre>
  
  <p>I know how I'd map this using XML mapping but I want to use the built-in mapping by code if at all possible.</p>