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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:43:08+00:00 2026-05-24T19:43:08+00:00

I am attempting to save(insert) a Parent entity with list Child entities. Both entites

  • 0

I am attempting to save(insert) a Parent entity with list Child entities. Both entites use a Guid as primary keys. The keys are none nullable and there is no key relationship set up in the db.

The save doesn’t work, an exception is thrown claiming – that I am trying to save a null into Parent foreign key in the Child table.

I was expecting nhibernate to create a key for the Parent and let its Child objects know about it. Is this a limitation of NHibernate or of using Guids as primary keys?

This answer suggests you have to set the parent manully, is this really the only way?

Here are my mappings for reference:

Parent mapping:

HasMany(x => x.Children).KeyColumn("ParentKey").Inverse().Cascade.All();

Child mapping:

 References(x => x.Parent).Not.Nullable().Column("ParentKey");
  • 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-24T19:43:09+00:00Added an answer on May 24, 2026 at 7:43 pm

    NHibernate is not magic. It is only an ORM, if your children do not have their reference set to the parent, why would it assume that because the parent has a list of children that the childrens’ reference, in turn, SHOULD be a reference to the parent?

    I believe you answered your own question when you stated that the Child entities don’t have their Parent property populated (which means it is null, which means NHibernate would be attempting to save a ‘null’ value for the Parent id in your Child table).

    If you were to use these objects WITHOUT NHibernate it would make sense that the Parent reference needs to be set on the Child entities when they are added.

    EDIT: This is for the case where you have specified ‘Inverse’ on your mapping. If you were to remove this call to ‘Inverse,’ it should work the way you wanted it to work as Inverse states that the other end (the child entity) is responsible for keeping track of the relationship. This means you need to set the reference of the parent on the child manually.

    However, removing the Inverse statement would cause the Child(ren) to be saved, the parent to be saved, then the Child(ren)’s parent id to be UPDATED. Since you have a null constraint on the parent id, this means it still won’t work as it would initially insert the Child with a parent id of null.

    The two solutions would be to either remove this constraint, or to just add a method to the Parent called AddChild:

    public void AddChild(Child childObj)
    {
        childObj.Parent = this;
        Children.Add(childObj);
    }
    

    Add another method called RemoveChild:

    public void RemoveChild(Child childObj)
    {
        if (Children.Contains(childObj))
        {
            Child.Parent = null;
            Children.Remove(childObj);
        }
    }
    

    Then, just use those methods for adding/removing children.

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

Sidebar

Related Questions

I'm attempting to use TinyXML to read and save from memory, instead of only
I am attempting to save keys to SharedPreferences during an onPause() event, but this
Occasionally while attempting to save a Crystal Report that I'm working on in VS2008,
I am attempting to save an rtf file using FileDialog and would like to
I'm attempting to save a c-style array of Vertex3D structs to an NSData object
To track user activity history on a web application, I am attempting to save
Im attempting to crop a pretty high res image and save the result to
I'm attempting to define an extremely simple utility method that will save me from
Attempting to insert an escape character into a table results in a warning. For
Attempting to print out a list of values from 2 different variables that are

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.