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

  • Home
  • SEARCH
  • 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 7623881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:49:06+00:00 2026-05-31T04:49:06+00:00

I have an application storing a tree structure and is able to generate the

  • 0

I have an application storing a tree structure and is able to generate the database fine, but when inserting I get an exception. I simplified the code to narrow down the problem.

The class to be saved:

public class Node
{
    public virtual int NodeId { get; set; }
    public virtual string Name { get; set; }
    public virtual Node Parent { get; set; }
    public virtual IList<Node> Children { get; set; }
}

This is the mapper code:

public sealed class NodeMap : ClassMap<Node>
{
    public NodeMap()
    {
        Id(n => n.NodeId).GeneratedBy.Identity();
        Map(n => n.Name);
        References(n => n.Parent).LazyLoad().Nullable();
        HasMany(n => n.Children).LazyLoad().KeyColumn("ParentId");
    }
}

When I do an insert like dataClient.Insert(new Node { Name = "Test" }); I get an exception saying:

NHibernate.Exceptions.GenericADOException: could not insert: [Model.Node][SQL: INSERT INTO [Node] (Name, NodeId) VALUES (?, ?); select SCOPE_IDENTITY()]

And the inner exception says:

System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table ‘Node’ when IDENTITY_INSERT is set to OFF.

Which is actually perfectly understandable, but I just wonder why it is trying to set an explicit value for NodeId. When I remove the References part, the insert goes through, so it must be related to that somehow.

The version of FluentNHibernate is 1.2.0.712 and the version of NHibernate is 3.1.0.4000.

  • 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-31T04:49:07+00:00Added an answer on May 31, 2026 at 4:49 am

    it is because the default column is typename + id for the reference which happens to be the same name as the id column. Explicitly state it in the reference.

    References(n => n.Parent, "ParentId").LazyLoad().Nullable();
    

    Update: as convention

    public class ReferenceColumnConvention : IReferenceConvention
    {
        public void Apply(IManyToOneInstance instance)
        {
            // uncomment if needed
            //if (instance.EntityType == instance.Property.PropertyType)
            instance.Column(instance.Name + "id");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a sharepoint based application that uses a custom database for storing metadata/files
Basically, I have an RPG-type Rails application. I'm storing skills in a skills database
I have an application storing the times a restaurant is open: My code to
I have two models in my Django application, for the purposes of storing search
I have an application which utilizes graph (tree-like) custom structures. The structures are not
I have an application which is currently using and storing data in Core Data.
We have an application that will be collecting data and storing it in local
I'm working on an application that generates a tree structure of nodes. There are
We have a typical multi-tier/layer architecture. Application + WCF Service + Repository/EF4/Database. We are
Scratching my head here: I have an application which works fine in Debug+Release if

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.