Disabling caching in Fluent NHibernate for a specific override

We’re using convention based mapping with Fluent NHibernate. The mapping looks like so:

  Table.Is(x => string.Concat(x.EntityType.Name.ToLower(), "s")),
  PrimaryKey.Name.Is(x => "Id"),
  Cache.Is(x => x.ReadWrite())

For most of our objects this is perfect but on certain objects I wish to disable the 2nd level cache. However it doesn’t appear that I can do this. There is no fluent option for Cache.None. I’ve even tried Not.Cache() but that didn’t work either.

How can one disable the cache for certain selected model objects?

Ok, I managed to find it after some digging around jogged an idea:

  1. Remove the shortcut “Cache.Is(x => x.ReadWrite())”
  2. Create a new convention class:

public class CacheableConvention: IClassConventionAcceptance, IClassConvention 
    public void Accept(IAcceptanceCriteria criteria)
        criteria.Expect(x => x.EntityType.IsNotAny(typeof(Content), typeof(InstanceSetting))); 

    public void Apply(IClassInstance instance)
  1. Add the convention to the AutoMappings.
  2. Done!
Explore posts in the same categories: Development, NHibernate


You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: