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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:50:44+00:00 2026-05-13T23:50:44+00:00

I have two entities: public class Parent() { public ICollection<Child> Children { get; set;

  • 0

I have two entities:

public class Parent()
{
    public ICollection<Child> Children { get; set; }
}

public class Child()
{
    public Parent Parent { get; set; }
}

The mapping looks like so:

public class ParentMap : ClassMap<Parent>
{
    HasMany(x => x.Children).Cascade.All().Inverse();
}

public class ChildMap : ClassMap<Child>
{
    References(x => x.Parent).Cascade.None();
}

Normally, in order to save a parent or a child, you’d have to do something like this:

var parent = new Parent();
var child = new Child();

parent.Children.Add(child);
child.Parent = parent;

Session.SaveOrUpdate(parent);

In other words, you have to set the properties in both objects in order for NHibernate to persist this entity correctly.

However, for legacy issues, I’d like to save this entity without having to set both sides, so that I can just do:

var parent = new Parent();
var child = new Child();
child.Parent = parent;

Session.SaveOrUpdate(child);

In this case, I don’t add the child to the parent’s Children collection, I just set the Parent on the child object.

Is there any way to get NHibernate to persist this correctly so that when I grab the parent out of the database, it will have the child inside its Children collection?

P.S. I realize that there’s a potential issue here with the session cache and relying on NHibernate to perform some behind-the-scenes operations to make the data model correct, but I’m ignoring this issue for now.

  • 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-13T23:50:44+00:00Added an answer on May 13, 2026 at 11:50 pm

    Map Parent as a private field in Child:

    public class ChildMap : ClassMap<Child>
    {
        References(x => x.Parent).Cascade.None()
            .Access.CamelCaseField(Prefix.Underscore);
    }
    

    and control it through the public property:

    public class Child()
    {
        private Parent _parent;
    
        public Parent Parent
        {
            get { return _parent; }
            set
            {
                _parent = value;
                _parent.Children.Add(this);
            }
        }
    }
    

    But the code you have now should work as expected. You don’t have to add the Child to the Parent’s Children collection to get NH to persist it because it’s marked as the inverse side of the relationship. You add it to keep the in-memory objects in sync with the database. If you set the Parent on the Child, flush the session, and re-load the parent in a new session then the Children collection should contain the child (assuming 2nd level cache doesn’t come into play).

    Edit: To add the child to the parent, encapsulate the action in a method:

    public void AddChild(Child child)
    {
        _children.Add(child);
        child.Parent = this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities, defined as following public class Corporation { public int Id{get;set;}
I have two entities like: public class Employee { public int Id { get;
For example I have two entities Class A { public Guid Id {get;set;} public
Say I have two Entities. public class Category{ public virtual int Id{get;set;} public virtual
I have two entities like this: public class Service { public virtual int ServiceId
I have two entities that are something like that: public class Movie { public
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have two entities: public class Product { [HiddenInput(DisplayValue=false)] public int ProductID { get;
I have following two entities public class User { public int UserId { get;
I have this two entities: public class Course { public int Id { get;

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.