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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:09:53+00:00 2026-05-26T11:09:53+00:00

I cannot find an example on this. I’m trying to get a basic understanding

  • 0

I cannot find an example on this. I’m trying to get a basic understanding of Fluent NHibernate but resources seem quite scarce in terms of proper tutorials.

I have a test class like this:

public class User
{
    public virtual long ID { get; set; }
    public virtual string Username { get; set; }
    public virtual MoreDetails ExtendedDetails { get; set; }    
}

With another class like this:

public class MoreDetails
{
    public virtual long ID { get; set; }
    public virtual string Firstname { get; set; }
    public virtual long UserID { get; set; } // Foreign key in the DB
}

What exactly should my mapping look like?

How can I query the DB properly with either Lazy or Eager loading to be able to do this:

// user object instantiated using your provided example:
userObject.ExtendedDetails.Firstname

I feel like an idiot.. normally I can follow documentation but its very vague with this sort of usage. Can anyone point me to a proper example (or give one)?

I’m using the latest Fluent NHibernate straight from the Fluent NHibernate website.

Regards,

chem

  • 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-26T11:09:54+00:00Added an answer on May 26, 2026 at 11:09 am

    Here is a good walk through that can help you get up and running: http://dotnetslackers.com/articles/ado_net/Your-very-first-NHibernate-application-Part-1.aspx

    Your mappings would look something like this. Note, that I’m making some assumptions on that how you would like to generate your identities along with the type of mapping between these entities.

    public class User
    {
        public int ID { get; set; }
        public string Username { get; set; }
        public MoreDetails ExtendedDetails { get; set; }
    }
    
    public class MoreDetails
    {
        public int ID { get; set; }
        public string Firstname { get; set; }
        public User User { get; set; } // Foreign key in the DB
    } 
    
    public UserMapping()
    {
        Not.LazyLoad();
        Id(e => e.ID).GeneratedBy.Identity();
        Map(e => e.Username).Not.Nullable();
        HasOne(x => x.ExtendedDetails)
            .Cascade
            .SaveUpdate();
    }
    
    public MoreDetailsMapping()
    {
        Not.LazyLoad();
        Id(e => e.ID).GeneratedBy.Identity();
        Map(e => e.Firstname).Not.Nullable();
        References(x => x.User).Column("UserID");
    }
    

    In order to query your relational data you will need to open a session within nhibernate. I generally create a helper along the lines of this.

     public class NHibernateHelper
        {
            private static ISessionFactory _sessionFactory;
    
            private static ISessionFactory SessionFactory
            {
                get
                {
                    if (_sessionFactory == null)
                        InitializeSessionFactory();
    
                    return _sessionFactory;
                }
            }
    
            private static void InitializeSessionFactory()
            {
                _sessionFactory = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008
                                  .ConnectionString(@"Server=localhost\SQLExpress;Database=SomeDB;Trusted_Connection=True;")
                                  .ShowSql()
                    )
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<User>())
                    .BuildSessionFactory();
            }
    
            public static ISession OpenSession()
            {
                return SessionFactory.OpenSession();
            }
        }
    

    From there you could directly query like this:

    public IQueryable<User> Users
            {
                get { return NHibernateHelper.OpenSession().Query<User>(); }
            }
    

    Note, that I’m leaving a lot out of this but hopefully this will assist you in getting up and running.

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

Sidebar

Related Questions

I cannot find the solution for this problem, here is the simplified example: On
I get this message: Cannot find the X.509 certificate using the following search criteria:
I have been searching for an answer to this but cannot find a simple
This seems like it should be simple, however, I cannot find an example through
I have seen somewhere this is 100% doable using blend behaviors, cannot find example.
Cannot find any example on Google.
I cannot find a simple example about my question above: how can i detect
I cannot find this in the documentation anywhere. Does anyone know if this is
I cannot find a way to get a Book's Ratings through the Books API
I cannot find EntityConfiguration. The same question for RC was here but I thought

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.