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 731457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:01:43+00:00 2026-05-14T07:01:43+00:00

My database is being driven by my NHibernate mapping files. I have a Category

  • 0

My database is being driven by my NHibernate mapping files.

I have a Category class that looks like the following :

public class Category {

        public Category() : this("") { }

        public Category(string name) {
            Name = name;
            SubCategories = new List<Category>();
            Products = new HashSet<Product>();
        }


        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual Category Parent { get; set; }
        public virtual bool IsDefault { get; set; }
        public virtual ICollection<Category> SubCategories { get; set; }
        public virtual ICollection<Product> Products { get; set; }   

and here is my Mapping file :

<property name="Name" column="Name" type="string" not-null="true"/>
<property name="IsDefault" column="IsDefault" type="boolean" not-null="true" />
<property name="Description" column="Description" type="string" not-null="true" />

<many-to-one name="Parent" column="ParentID"></many-to-one>

<bag name="SubCategories" inverse="true">
  <key column="ParentID"></key>
  <one-to-many class="Category"/>
</bag>
<set name="Products" table="Categories_Products">
  <key column="CategoryId"></key>
  <many-to-many column="ProductId" class="Product"></many-to-many>
</set>

when I try to create the database I get the following error :

failed: The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint “FK9AD976763BF05E2A”. The conflict occurred in database “CoderForTraders”, table “dbo.Categories”, column ‘CategoryId’.
The statement has been terminated.

I looked on the net for some answers but found none.
Thanks for your help

Here is the missing part :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CBL.CoderForTraders.DomainModel" namespace="CBL.CoderForTraders.DomainModel" default-access="field.camelcase-underscore" default-lazy="true">

  <class name="Category" table="Categories" >
    <id name="_persistenceId" column="CategoryId" type="Guid" access="field" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="assigned" />
    </id>
    <version name="_persistenceVersion" column="RowVersion" access="field" type="int" unsaved-value="0" />
  • 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-14T07:01:44+00:00Added an answer on May 14, 2026 at 7:01 am

    The error is occurring on an insert, not table creation. You don’t show the mapping for ID, but if it’s an identity column in SQL Server then it’s impossible to insert the first record because a parent record (Category property) doesn’t exist. One solution might be to temporarily drop the constraint, insert a “root” record that refers to itself, then add the constraint back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string being returned from my database that I want to use
I have developed a web application driven by a mysql database that stores information
We have a custom software project that's database driven (MySQL) and is built with
I am writing a few test cases that depend on the database being available,
I would like to find out if any and which database links are being
Being stuck with a legacy database schema that no longer reflects your data model
I have an object list being loded from a database to a drop down
I have databases which manage files. The reason that there are multiple is that
Let's say we have a system like this: ______ { application instances ---network--- (______)
Here's the problem I'm trying to solve: I have a dynamic php-driven website that

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.