具有流畅的nHibernate自动化的属性过滤器

时间:2011-04-20 10:35:45

标签: nhibernate fluent-nhibernate

我正在尝试创建一个过滤器,使用nH 2.1.2自动化流畅的nH(1.2) 我遵循了示例here,但我一直得到例外:

filter-def for filter named 'DateFilter' was never used to filter classes nor collections..  

过滤器类:

public class DateFilter : FilterDefinition
    {
        public DateFilter()
        {
            WithName(Consts.FilterConsts.DATE_FILTER)
                .AddParameter("date", NHibernate.NHibernateUtil.DateTime)
                .WithCondition("DATEPART(dayofyear,EntityTime) = DATEPART(dayofyear, :date)")
                ;
        }
    }

并在映射覆盖中:

mapping.HasMany(x => x.Stuff)
                .LazyLoad()
                .ReadOnly()
                .ApplyFilter<DateFilter>();

这是我的配置代码。

Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                .DefaultSchema("dbo")               //set default schema to enable full-qualified queries
                .AdoNetBatchSize(batchSize > 0 ? batchSize : 1)
                .UseReflectionOptimizer()
                .ConnectionString(c => c.FromConnectionStringWithKey(connectionStringKey))
                    .Cache(c => c.UseQueryCache()
                                    .ProviderClass(
                                    isWeb ? typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName //in web environment- use sysCache2
                                        : typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName //in dev environmet- use stupid cache
                                    )) 
                          )
                 .Mappings(m => m.AutoMappings.Add(
                    AutoMap.AssemblyOf<Domain.Entity>(cfg)     //automapping the domain entities
                    .IncludeBase<Domain.SomethingBase>()               //ensure that although SomethingBase is a base class, map it as well. this enables us to store all Something sub-classes in the same table
                    .IncludeBase<Domain.OrOtherBase>()    //create a table for the abstract 'OrOtherBase' class
                    .UseOverridesFromAssemblyOf<MappingOverrides.MappingOverride>()
                    .Conventions.Add(DefaultCascade.All())      //make sure that all saves are cascaded (i.e when we save a zone, its queues are saved as well)
                    .Conventions.AddFromAssemblyOf<IdGenerationWithHiLoConvention>()
                    ))
                 .Mappings(m => m.FluentMappings.Add(typeof(DateFilter)));

如果我在自动化部分之前移动线,我得到例外:

 NHibernate.MappingException: filter-def for filter named 'DateFilter' was not found.  
谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:3)

好的,所以我想出来了。当您单独添加映射时,它们会以不同的映射结束,并且会抱怨您从不使用过滤器或抱怨它找不到过滤器,因为它不会同时查找这两个位置。解决方案是将其直接添加到automap中,因此在您的情况下如下:

//other stuff up here
.Mappings(m => m.AutoMappings.Add(() => {
    var a = AutoMap.AssemblyOf<Domain.Entity>(cfg) 
                .IncludeBase<Domain.SomethingBase>() //and also cascades and conventions and stuff
    a.Add(typeof(DateFilter));
    return a;
 }));

有点严重,因为.Add()不流利,但确实有效。