1

What Im trying to do is this: http://www.janblaha.net/blog/nhibernate-multitenancy-in-shared-database

This is my filter definition:

public class MultitenancyFilter : FilterDefinition
    {
        public MultitenancyFilter()
        {
            WithName("multitenancy").WithCondition("TenantId= :tenantId").AddParameter("tenantId", NHibernate.NHibernateUtil.Int32);
        }
    }

Then, I do my query like this:

_session.EnableFilter("multitenancy").SetParameter("tenantId", tenantId);
var people = _session.QueryOver<Person>().List().ToList();

where tenantId is some int.

But the NHibernate never generates the filter in the query. The query is generated without 'where' clausure at all.

Am I missing some configuration or some concept?

NHibernate Version 4.0.0.4000.

PS. When debuggin, the _session has a property called EnabledFilter, and my filter is listed there. So, whats wrong?

RenanStr
  • 1,208
  • 1
  • 13
  • 15

1 Answers1

1

According with NHibernate documentation you can attach the filter to your class after having the definition:

<class name="MyClass" ...>
    ...
    <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
</class>

If you are using Fluent NHibernate you can also use this reference.

Najera
  • 2,869
  • 3
  • 28
  • 52
  • I am using Fluent NHibernate. ApplyFilter("PonyCondition = :condition"); in ClassMap solved the problem. Thanks. But .WithCondition("TenantId= :tenantId") in FilterDefinition shouldnt do this for me?!?! – RenanStr Oct 30 '17 at 16:25
  • Appeartenly it need to be specified per class so if you are using Fluent NHibernate you can create a base class map to attach the filter to all your classes, or by a convention https://askpoya.wordpress.com/2014/02/04/nhibernate-web-api-multitenancy-2/ – Najera Oct 30 '17 at 17:05