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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:46:19+00:00 2026-05-10T20:46:19+00:00

Basic question : How to I create a bidirectional one-to-many map in Fluent NHibernate?

  • 0

Basic question: How to I create a bidirectional one-to-many map in Fluent NHibernate?

Details:

I have a parent object with many children. In my case, it is meaningless for the child to not have a parent, so in the database, I would like the foreign key to the parent to have NOT NULL constraint. I am auto-generating my database from the Fluent NHibernate mapping.

I have a parent with many child objects like so:

public class Summary {    public int id {get; protected set;}     public IList<Detail> Details {get; protected set;} }  public  class Detail {    public int id {get; protected set;}     public string ItemName {get; set;}    /* public Summary Owner {get; protected set;} */ //I think this might be needed for bidirectional mapping? } 

Here is the mapping I started with:

public class SummaryMap : ClassMap<Summary> {     public SummaryMap()     {         Id(x => x.ID);          HasMany<Detail>(x => x.Details);     } }  public class DetailMap : ClassMap<Detail> {     public DetailMap()     {         Id(x => x.ID);          Map(x => x.ItemName).CanNotBeNull();     } } 

In the Detail table, the Summary_id should be Not Null, because in my case it is meaningless to have a Detail object not attached to the summary object. However, just using the HasMany() map leaves the Summary_id foreign key nullable.

I found in the NHibernate docs (http://www.hibernate.org/hib_docs/nhibernate/html/collections.html) that ‘If the parent is required, use a bidirectional one-to-many association’.

So how do I create the bidirectional one-to-many map in Fluent NHibernate?

  • 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. 2026-05-10T20:46:20+00:00Added an answer on May 10, 2026 at 8:46 pm

    To get a bidirectional association with a not-null foreign key column in the Details table you can add the suggested Owner property, a References(…).CanNotBeNull() mapping in the DetailsMap class, and make the Summary end inverse.

    To avoid having two different foreign key columns for the two association directions, you can either specify the column names manually or name the properties in a way that gives the same column name for both directions. In this case you I suggest renaming the Details.Owner property to Details.Summary.

    I made the Summary id generated by increment to avoid problems when inserting into the table since Summary currenty has no columns besides id.

    Domain:

    public class Detail {     public int id { get; protected set; }     public string ItemName { get; set; }      // Renamed to use same column name as specified in the mapping of Summary.Details     public Summary Summary {get; set;}  }  public class Summary {     public Summary()     {         Details = new List<Detail>();     }      public int id { get; protected set; }     public IList<Detail> Details { get; protected set; } } 

    Mapping:

    public class DetailMap : ClassMap<Detail> {     public DetailMap()     {         Id(x => x.id)             .GeneratedBy.Native();          Map(x => x.ItemName)             .CanNotBeNull();          References<Summary>(x => x.Summary)             // If you don't want to rename the property in Summary,             // you can do this instead:             // .TheColumnNameIs('Summary_id')             .CanNotBeNull();     } }  public class SummaryMap : ClassMap<Summary> {     public SummaryMap()     {         Id(x => x.id)             .GeneratedBy.Increment();          HasMany<Detail>(x => x.Details)             .IsInverse()             .AsBag(); // Use bag instead of list to avoid index updating issues     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 138k
  • Answers 138k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, your fundamental aim seems like madness, but with your… May 12, 2026 at 7:36 am
  • Editorial Team
    Editorial Team added an answer Edit: If it turns out you are having user agent… May 12, 2026 at 7:36 am
  • Editorial Team
    Editorial Team added an answer Check the post_max_size option in php.ini. That must be larger… May 12, 2026 at 7:36 am

Related Questions

This is a very basic question. I'm just on my mission to learn ASP.NET
How can I assign a keyboard shortcut to a VBA macro in Outlook 2007?
I have this problem to solve that I have no idea how to do
I write the code for my own website as an educational/fun exercise. Right now

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.