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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:37:28+00:00 2026-05-15T16:37:28+00:00

I want to be able to create a parent object with children and let

  • 0

I want to be able to create a parent object with children and let the parent handle the update, saves and deletes.

My map classes.

ParentMap

    Id(x => x.Id, "ID").GeneratedBy.Identity();
        Map(x => x.Name);                

        HasMany(x => x.Children)
            .KeyColumn("ParentID")
            .Inverse()
            .Cascade
            .AllDeleteOrphan()
            .AsBag();

ChildMap

Id(x => x.Id, "ID").GeneratedBy.Identity();
Map(x => x.Name);            
Map(x => x.Value);            
References(x => x.Parent);

This is the code.

After the postback I create a new parent with children. The problem is it won’t delete the children but everything else works fine update and saves.

var parent = new Parent();
parent.Id = _view.parentID;
parent.Name = _view.Name;
parent.Children = _view.Children; 

I’ve also tried the code below but this returns a nonunique error.

var parent = repository.Get(_view.parentID);
parent.Name = _view.Name;
parent.Chidlren = _view.Children;

Can anyone tell me the best way to deal with this in NHibernate?

Thanks.

  • 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-15T16:37:29+00:00Added an answer on May 15, 2026 at 4:37 pm

    You can’t reassign the child collection with NHibernate. NHibernate is basically “watching” the child collection for changes so that it knows how to handle saves to the database. If you reassign the child collection NHibernate looses the reference and can no longer track the changes. To get around this you must modify the child collection but not reassign it.

    How I normally ensure this is I make the collection readonly with a private setter. I then add methods to the parent class to modify the list as needed.

    private IList<ChildRecord> theChildCollection = new List<ChildRecord>();
    /// <summary>
    /// The collection of child records.
    /// </summary>
    public virtual IList<ChildRecord> Children
    {
       get
       {
          return theChildCollection.ToList().AsReadOnly();
       }
       private set
       {
          theChildCollection = value;
       }
    }
    
    /// <summary>
    /// Adds a record to the child collection.
    /// </summary>
    public void Add(ChildRecord aRecord)
    {
       theChildCollection.Add(aRecord);
    }
    
    /// <summary>
    /// Removes a record from the child collection.
    /// </summary>
    public void Delete(ChildRecord aRecord)
    {
       theChildCollection.Remove(aRecord);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hell all, I am currently trying to create a few object dynamically in code.
I want to create a Category class that may or may not have a
I want to be able to use methods that I haven't required() at the
I want to be able to assign different images to my tabs in the
I have a list of Child objects. I would like to be able to
I have a website that consists of one parent page. Using the lightbox-style jQuery
code is given below, problem is i am not able to remove Main tab
I've been playing with jquery for a week or two now and have something
I am developing a WPF Application using MVVM Architecture. I am an amateur in
Is there an accepted method of adding possibly-derived objects to a collection without allowing

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.