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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:19:44+00:00 2026-05-20T11:19:44+00:00

I have the following models & mappings (code snippets further below). One Competition has

  • 0

I have the following models & mappings (code snippets further below).

One Competition has to have multiple CompetitionAnswers associated with it (multiple choice) from the outset.

At present, using the Fluent NHibernate mappings shown below, when I create a brand new Competition object, populate the properties, then create 3 brand new CompetitionAnswer objects and add them to the CompetitionAnswers property (property on Competition), I would expect to call Save on the session which would INSERT the 1 Competition row and 3 CompetitionAnswer rows to the DB.

However, as soon as I try to call Save on the session, it complains that CompetitionId is null and it can’t insert a null into the CompetitionAnswers table for that field – which is right, it shouldn’t, however, I assumed that the NHibernate would first create the Competition, then use the newly generated IDENTITY value (CompetitionId) in the CompetitionAnswers table?

Competition (Model)

public virtual int CompetitionId { get; private set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual IList<CompetitionAnswer> CompetitionAnswers { get; set; }

CompetitionAnswer (Model)

public virtual int CompetitionAnswerId { get; set; }
public virtual string Answer { get; set; }
public virtual Competition Competition { get; set; }

CompetitionMap (Fluent NHibernate Mapping)

public CompetitionMap()
{
    Id(x => x.CompetitionId)
        .GeneratedBy.Native();
    Map(x => x.Title);
    Map(x => x.Description);
    HasMany(x => x.CompetitionAnswers)
        .Cascade.AllDeleteOrphan()
        .KeyColumn("CompetitionId")
        .Inverse();
    Table("Competitions");
}

CompetitionAnswerMap (Fluent NHibernate Mapping)

public CompetitionAnswerMap()
{
    Id(x => x.CompetitionAnswerId)
        .GeneratedBy.Native();
    Map(x => x.Answer);
    References(x => x.Competition)
        .Column("CompetitionId");
    Table("CompetitionAnswers");
}

Here is some sample code that I’ve used to test this scenario, which generates the error:

Competition c = new Competition();

c.Description = "Description";
c.Title = "Title";

CompetitionAnswer a1 = new CompetitionAnswer { Answer = "Answer 1" };
CompetitionAnswer a2 = new CompetitionAnswer { Answer = "Answer 2" };
CompetitionAnswer a3 = new CompetitionAnswer { Answer = "Answer 3" };

c.CompetitionAnswers.Add(a1);
c.CompetitionAnswers.Add(a2);
c.CompetitionAnswers.Add(a3);

session.Save(c);

The exact error that I get as soon as it tries to Save is:

Cannot insert the value NULL into
column ‘CompetitionId’, table
‘CompetitionAnswers’; column does not
allow nulls. INSERT fails. The
statement has been terminated.

Can anyone please shed any light on why this isn’t currently working?

  • 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-20T11:19:44+00:00Added an answer on May 20, 2026 at 11:19 am

    I’m pretty sure, not 100%, that the problem is the Inverse() specification in your mapping of CompetitionAnswers on Competition. Inverse() specifies that the child records are responsible for defining their relationship to the parent. Most often, the “one” side of a one-to-many (the parent) is the “top” of an object graph and “owns” the relationship with its children. Parents have children, and the decision regarding whether to keep or give away the child for adoption is the parent’s. However, this isn’t always the case; a college may have students, but it’s the students who have the real power to decide where they will go. Here, the Student is the “top” of the graph, and the School is just a monolithic record identifying the Student’s attendance. The Student can transfer at any time; it’s their decision, and it doesn’t really change the School in any meaningful way, so the Students are responsible for identifying themselves as belonging to the School.

    Your case is the first one: Competitions have CompetitionAnswers, and the child doesn’t logically have the responsibility of saying “I belong to a Competition”; the Competition instead “owns” its collection of answers. Removing the Inverse() instruction should make NH treat Competition as the “top” of the object graph, so NH will insert the Competition, then the CompetitionAnswers, which can now reference their parent’s ID.

    Another thing not related to the problem, but if you’re mapping to an MS SQL Server database, and the ID column is defined as an identity column in the DB, I’d specify GeneratedBy.Identity() for the ID columns. Native() SHOULD end up using Identity, but it will also check to see if HiLo or Sequence methods are available.

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

Sidebar

Related Questions

Im running into a small (newbie) problem. I've got 2 models: question & reviews.
I have a to-one relationship between a Document and Settings model: On creation of
My problem is hydrating a Viewmodel from a Linq2Sql object that has been returned
I'm kind of stuck working out where I'm going wrong with the below entity
I have a master page which I am using as a template to allow
I've got the following entities on my EDMX :- These two entites were generated
I have a pretty simple setup that I cannot get to work in silverlight.
I have an iPhone app that is using CoreData. I recently made some minor
I am looking for an elegant solution for the following problem. Let's assume we
I have a background service running which sends out emails to users of my

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.