Disabling caching in Fluent Nhibernate for a specific override

僤鯓⒐⒋嵵緔 提交于 2020-01-04 08:12:11

问题


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

            .Conventions.Add
            (
                Table.Is(x => string.Concat(x.EntityType.Name.ToLower(), "s")),
                PrimaryKey.Name.Is(x => "Id"),
                DefaultLazy.Always(),
                DefaultCascade.SaveUpdate(),
                AutoImport.Never(),
                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.

Has anyone got any ideas on how I can disable the cache for certain selected model objects?


回答1:


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), typeof(Profanity))); 
    }

    public void Apply(IClassInstance instance)
    {
        instance.Cache.ReadWrite();
    }
}
  1. Add the convention to the AutoMappings.
  2. Done!


来源:https://stackoverflow.com/questions/13856389/disabling-caching-in-fluent-nhibernate-for-a-specific-override

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!