Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6474453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:35:19+00:00 2026-05-25T06:35:19+00:00

Took me a while to find an answer for this so thought I’d share

  • 0

Took me a while to find an answer for this so thought I’d share the love.


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):

“Cannot insert explicit value for identity column in table ‘DietUser’ when IDENTITY_INSERT is set to OFF.”

My table uses an identity ID and the entity & mappings look like this:

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<User>
{
    public UserMapping()
    {
        Id(x => x.Id);
        Property(x => x.Name);
        Property(x => x.Username);
        Property(x => x.Password);
        Property(x => x.DailyPoints);
    }
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:35:20+00:00Added an answer on May 25, 2026 at 6:35 am

    After some digging around in the forums I got lucky and found how to map this. The example Fabio provides is great if you’re going to use a GUID for an ID or something like that but if you want to use an identity ID (saw a bunch of others as well), you need to specify what generator you’re going to use. Here’s my updated mapping that works:

    public class UserMapping : ClassMapping<User>
    {
        public UserMapping()
        {
            Id(x => x.Id, map => map.Generator(Generators.Identity));
            Property(x => x.Name);
            Property(x => x.Username);
            Property(x => x.Password);
            Property(x => x.DailyPoints);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Took me a while to find this bug in my code. Trying to create
This took me a while to figure out and I found the answer on
This bug took me a while to find... Consider this method: public void foo(Set<Object>
Okay, it took me a little while to narrow down this problem, but it
I took a look at this answer and it goes in part to solving
It took me FOREVER to finally find code that can download a doc file
I've been searching Google for a while now to find pseudo-code of a decently
I have the following text from an academic course I took a while ago
I took the plunge this afternoon and began studying LINQ, so far just mucking
It took me forever to reduce the problem to this. I cannot express the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.