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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:39:03+00:00 2026-05-24T04:39:03+00:00

I have a couple of domain objects which I am trying to save using

  • 0

I have a couple of domain objects which I am trying to save using the Cascade functionality. However, when I add a child to a parent and save the parent, the child object is not saved.

Update: The original version of the code here did not flush the session, which could have explained the behavior I saw. It didn’t, however, and I’ve updated the code in the quesiton to better reflect the actual situation.

This is what I do in my controller and repository:

// in controller
public ActionResult AddChildToParent(int id /* of parent */, ChildInputModel input)
{
    var child = GetChildFromInputModel(input); // pseudo
    var savedchild = _repo.AddChildToParent(c, id);

    // savedchild.Id is still 0, and nothing has been saved to the database.
    return View(savedchild);
}    

// in repo
public IChild AddChildToParent(IChild c, int parentId)
{
    var parent = GetParentById(parentId);
    var child = new Child();
    c.CopyValuesTo(child); // A utility function in our library
    parent.AddChild(child);

    // These two calls might be unnessesary, but I'm keeping them here to sho
    // my current code situation
    var session = GetNHibernateSession();
    session.Save(parent);

    return child;
    // session flushes when method returns thanks to injected transactions
}

I don’t see the error in my application until I’m redirected to the edit page and the ID is 0, but I’ve verified in the database that no child record is added. I’m not changing anything else on the parent – just adding a child.

What am I doing wrong here?

These are my entities and maps (simplified):

public class Parent : IParent // Interface just defines everything public
{
    public int Id { get; set; }
    public string Name { get; set; }

    private IList<Child> _children;
    public IList<Child> Children
    { 
        get 
        { return _children ?? (_children = new List<Child>()); } 
    }
    public void AddChild(Child c)
    {
         this.Children.Add(c);
         c.Parent = this;
    }
}

public class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        Id(p => p.Id);
        Map(p => p.Name);

        HasMany(p => p.Children).Cascade.AllDeleteOrphan().Not.LazyLoad();
    }
}

public class Child : IChild // Interface just defines everything public
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Parent Parent { get; set; }
}

public class ChildMap : ClassMap<Child>
{
    public ChildMap() 
    {
        Id(c => c.Id);
        Map(c => c.Name);
        References(c => c.Parent).Not.Nullable();
    }
}
  • 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-24T04:39:03+00:00Added an answer on May 24, 2026 at 4:39 am

    That’s normal: cascades are not stored immediately. The children get the ids after the next flush (which may take time, so don’t flush explicitly after each call to AddChild) or after explicitly saving the child.

    var parent = GetParentById(parentId);
    var child = new Child();
    c.CopyValuesTo(child); // A utility function in our library
    parent.AddChild(child);
    
    var session = GetNHibernateSession();
    // no need to save the parent, since it is already in the session.
    // you may optionally save the child when the id is used in the code
    session.Save(child);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain object that has a couple of Uri properties that need
I am using Silverlight with a DDD approach. I have a Person domain object
I have a domain class containing a couple of fields. I can access them
I have couple resource DLLs that I currently load when application starts using following
Here's the problem: I have couple of pages which gets its content from a
I have had couple of encounters of this and what I am trying to
I have a couple of sub-domains on my rails app, and the main domain
I am exposing a couple domain objects via a SOAP based web service. Some
I have a descriptor class for a couple of domain classes. The descriptor class
I have seen more than a couple of sites using flash content and displaying

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.