2

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

chemicalNova
  • 831
  • 5
  • 12

2 Answers2

2

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.

Jesse
  • 8,223
  • 6
  • 49
  • 81
  • This is fine for loading Users, but I still get a NullReference exception when attempting userObject.ExtendedDetails.Firstname.. do I not have to map the ExtendedDetails member of the User class? – chemicalNova Oct 25 '11 at 04:08
  • @chemicalNova Sorry, I failed to copy over part of the UserMapping() class. Updated answer. – Jesse Oct 25 '11 at 19:03
0

If your db really does have a one-to-one mapping between two tables you need to use HasOne in your ClassMap e.g. HasOne(x => x.ExtendedDetails)

See http://wiki.fluentnhibernate.org/Fluent_mapping for details on One-to-One mappings

Alex Norcliffe
  • 2,439
  • 2
  • 17
  • 21
  • How do I use that afterwards though? So User needs to have the HasOne map, does MoreDetails need anything? (like, References()?) The reason I ask is because I'm still getting a NullReferenceException on the ExtendedDetails. If HasOne() in the User class is all I need.. how do I make NHibernate load the relational data? – chemicalNova Oct 25 '11 at 03:23