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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:43:29+00:00 2026-06-06T07:43:29+00:00

Typical problem – I am trying to save new parent object with nested new

  • 0

Typical problem – I am trying to save new parent object with nested new IEnumerable of child objects which all have reference columns to store the parent Id but I can’t get NHibernate configured properly to save the children objects.

The reason it won’t save is because the column on the child table that references the parent table record ID is set to not allow null. But I can’t figure out the correct setup on my HasMany that will allow NHibernate to generate the parent record ID and provide that in the child records reference column.

Therefore I end up with “Cannot insert NULL into column…..” errors.

So I have read a ton of Questions/Answers here on Stackoverflow, and went through Ayende Rehien’s as well as Fluent’s Wiki and I have tried a number of different setups to no avail (i.e. remove the inverse, different cascade options..).

Currently my Entities look like this:

 public class Product
 {
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
   public virtual IEnumerable<Attribute> Attributes { get; set; }
}

public class Attribute
{
    public virtual int Id { get; set; }
    public virtual Product Product { get; set; }
    public virtual IEnumerable<AttributeValue> Values { get; set; }
}

public class AttributeValue
{
    public virtual int Id { get; set; }
    public virtual Attribute Attribute { get; set; }
    public virtual string Value { get; set; }
}

And my Fluent mappings look like:

public class ProductMap : ClassMap<Product>
{
  public ProductMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    Map(x => x.Description);
    HasMany(x => x.Attributes).Inverse().Cascade.All();
   }
 }

public class AttributeMap : ClassMap<Attribute>
{
    public AttributeMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        References(x => x.Product);
        HasMany(x => x.Values).Inverse().Cascade.All();
    }
}

public class AttributeValueMap : ClassMap<AttributeValue>
{
    public AttributeValueMap()
    {
        Id(x => x.Id);
        Map(x => x.Value);
        References(x => x.Attribute);
    }
}

My DB schema is set to not allow nulls for the reference column on the Attributes and AttributesValues table, but I have tried it as allow null because I had read that it could be setup to go back and update but that was not the case.

I have not been able to locate why I can’t save child objects and have Nhibernate save the parent ID on the referenced field. I have seen a number of examples and they never had to set a foreign key up, and reference it in the mapping.

Any thoughts?

  • 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-06-06T07:43:33+00:00Added an answer on June 6, 2026 at 7:43 am

    Of course I found the answer on the 16th article I read that that I had to have the reference columns in the database allow null = true as well as inverse needed to be removed. When I had attempted my test without inverse specified (not only did I not have a clear understanding as referenced in this answer) but also I didn’t have my reference columns set to allow nulls.

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

Sidebar

Related Questions

I have a typical problem which is like the following: 1) I was working
All, I'm new to XSLT and facing a typical problem. Below is the problem
A typical problem in OO programming is the diamond problem. I have parent class
I have following problem. I have typical master/detail UI scenario. I set main object
New to DDD here and have a architecture question which should be a typical
I've got a typical problem when trying to insert a date into MySQL. The
The problem I'm running into the typical virtual-directory dilemma in that you have some
I have to fix a typical memory leak, Problem is like that : typedef
I have a typical problem with SVN. I did a search in this repository
In typical Persian fonts which are True Type, there is a historical problem with

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.