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 61k
  • Answers 61k
  • 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
  • added an answer I've documented how I used Lucene.NET (in BugTracker.NET) here: http://www.ifdefined.com/blog/post/2009/02/Full-Text-Search-in-ASPNET-using-LuceneNET.aspx May 11, 2026 at 9:51 am
  • added an answer I've not come across any such limit and I doubt… May 11, 2026 at 9:51 am
  • added an answer No, javascript-triggered changes to form elements don't trigger events. If… May 11, 2026 at 9:51 am

Related Questions

Basic question : How to I create a bidirectional one-to-many map in Fluent NHibernate?
I've got a pretty basic question about how to start using Castle Windsor. I've
Very basic question: how do I write a short literal in C++? I know
My basic question is, in .NET, how do I clone WebControls? I would like
It's a really basic question but i can't think at the second. How do
Sorry for the basic question - I'm a .NET developer and don't have much
Kind of a basic question but I'm having troubles thinking of a solution so
This is a fairly basic question, which for some reason, a proper solution escapes
This is a really basic question but this is the first time I've used
This is a really basic question but... I have some code like this var

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.