Explanation of NHibernate HiLo

I believe your understanding is more or less correct. The max_lo parameter is simply used to determine the number of Ids available for any given Hi value. My best guess is that NHibernate’s default max_lo value is 32768. Thus a Hi value of 1 would start your Ids at 32768 and run you right up … Read more

Eagerly fetch multiple collection properties (using QueryOver/Linq)?

I prefer to use the linq provider if at all possible especially if you are using newer versions of nhibernate (>= 4.0). As long as you have your collections mapped as ISets (requires .net framework >= 4) which we converted to such that we could do eager loading and avoid cartesian products. I feel like … Read more

Difference between Get and Load

The reference provided by Brian explains it quite clearly. However, the main difference is that Load doesn’t hit the database to check and load the entity you require, since it assumes you know the entity exists. The object returned by Load is some kind of proxy that lazily fetches the real data when required or … Read more

Best way to delete all rows in a table using NHibernate?

In the TearDown of my UnitTests, I mostly do this: using( ISession s = … ) { s.Delete (“from Object o”); s.Flush(); } This should delete all entities. If you want to delete all instances of one specific entity, you can do this: using( ISession s = …. ) { s.Delete (“from MyEntityName e”); s.Flush(); … Read more

Identifying NHibernate proxy classes

You can detect if a class is a NHibernate proxy by casting it to (unsurprisingly) INHibernateProxy. If you need to get the underlying “real” object, use: Session.GetSessionImplementation().PersistenceContext.Unproxy(proxiedObject) You don’t need to test for proxies to call Unproxy; it returns the original parameter if it’s not a proxy. Edit: I now use a different approach to … Read more

What are the differences between HasOne and References in nhibernate?

HasOne creates a one-to-one mapping between tables for you. References creates a typical relational many-to-one relationship. More defined: a one-to-one relationship means that when one record exists in one table, it must (or can) have one and at most one record in the other referenced table. Example: User table and Options table (one user has … Read more

Fluent NHibernate – Create database schema only if not existing

You can just use SchemaUpdate instead, it will update the schema if it exists and create it if it does not: public NhibernateSessionFactory(IPersistenceConfigurer config) { _sessionFactory = Fluently.Configure(). Database(config). Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()). ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)). BuildSessionFactory(); } One caveat: SchemaUpdate does not do destructive updates (dropping tables, columns, etc.). It will only add … Read more