Get Distinct result set from NHibernate using Criteria API?

To perform a distinct query you can set the projection on the criteria to Projections.Distinct. You then include the columns that you wish to return. The result is then turned back into an strongly-typed object by setting the result transformer to AliasToBeanResultTransformer – passing in the type that the result should be transformed into. In … Read more

Difference between FluentNHibernate and NHibernate’s “Mapping by Code”

Fluent NH Fluent NHibernate offers an alternative to NHibernate’s standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code. vs. NH’s new mapping by code It is an XML-less mapping solution being an integral part of NHibernate … Read more

Install NHibernate 3.2 with NuGet

NHibernate 3.2 comes with its own proxy factory. If you’re using a config file, you just need to remove the proxyfactory configuration property. I believe the version of Fluent NHibernate that you’re using defaults to use NHibernate.ByteCode.Castle. In that case, you would need to override that setting with the built in NHibernate 3.2 proxy factory: … Read more

JSON.NET and nHibernate Lazy Loading of Collections

I was facing the same problem so I tried to use @Liedman’s code but the GetSerializableMembers() was never get called for the proxied reference. I found another method to override: public class NHibernateContractResolver : DefaultContractResolver { protected override JsonContract CreateContract(Type objectType) { if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType)) return base.CreateContract(objectType.BaseType); else return base.CreateContract(objectType); } }

How can C# nullable value typed values be set on NHibernate named IQuery parameters?

OK, it turns out there are some overrides on SetParameter that allow the type to be set explicitly. For example: query.SetParameter(position, null, NHibernateUtil.Int32); The full extension methods (for Int32 and DateTime only) are now: public static class QueryExtensions { public static void SetInt32(this IQuery query, int position, int? val) { if (val.HasValue) { query.SetInt32(position, val.Value); … Read more

ADO.NET Entity Framework vs NHibernate [closed]

NHibernate may be more mature. That does not necessarily mean it is a “better” solution. Having used it at my job for some time, I would personally prefer to use almost anything than NHibernate (even straight SQL, if migration were remotely feasible). The number of error messages thrown by NHibernate that don’t mean anything (or … Read more

NHibernate with TransactionScope

I have been using nHibernate 2.1 for awhile, and after a few production issues and trying quite a few variations, we have settled on the following method, as per Avoiding Leaking Connections With NHibernate And TransactionScope: using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (var session = sessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { // … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)