How to create NHibernate HasManyToMany relation

In this case the answer is pretty simple. Do not use many-to-many. Use pairing object. Exactly for the reasons you’ve mentioned: Extend the pairing object with more properties: Check here 24. Best Practices, a cite: Don’t use exotic association mappings. Good usecases for a real many-to-many associations are rare. Most of the time you need … Read more

Syntax to define a NHibernate Filter with Fluent Nhibernate?

If you build Fluent from source, there is now support for filters. You use them like this: First create a class inheriting from FluentNHibernate.Mapping.FilterDefinition: using FluentNHibernate.Mapping; namespace PonyApp.FluentFilters { public class PonyConditionFilter : FilterDefinition { public PonyConditionFilter() { WithName(“PonyConditionFilter”) .AddParameter(“condition”,NHibernate.NHibernateUtil.String); } } } In your ClassMap for your class, use the ApplyFilter method: namespace PonyApp.Entities.Mappings … Read more

How do add NOLOCK with nHibernate?

SetLockMode(LockMode.None) or connection.isolation ReadUncomitted does NOT append a NOLOCK to your queries. Ayende goes into the correct answer on his blog: If you’re using <sql-query> you can do the following: <sql-query name=”PeopleByName”> <return alias=”person” class=”Person”/> SELECT {person.*} FROM People {person} WITH(nolock) WHERE {person}.Name LIKE :name </sql-query> Note the WTIH(nolock) appended to the FROM clause.

How can I have NHibernate only generate the SQL without executing it?

You can get the generated sql queries without execution with the following methods: For the NHibernate.Linq queries: public String GetGeneratedSql(System.Linq.IQueryable queryable, ISession session) { var sessionImp = (ISessionImplementor) session; var nhLinqExpression = new NhLinqExpression(queryable.Expression, sessionImp.Factory); var translatorFactory = new ASTQueryTranslatorFactory(); var translators = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImp.EnabledFilters, sessionImp.Factory); return translators[0].SQLString; } For Criteria queries: public … Read more

How do you map an enum as an int value with fluent NHibernate?

The way to define this convention changed sometimes ago, it’s now : public class EnumConvention : IUserTypeConvention { public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) { criteria.Expect(x => x.Property.PropertyType.IsEnum); } public void Apply(IPropertyInstance target) { target.CustomType(target.Property.PropertyType); } }