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

发布于 2024-11-02 07:55:03 字数 2889 浏览 1 评论 0原文

我正在尝试使用流畅的 nH (1.2) 自动映射和 nH 2.1.2 创建一个过滤器。
我遵循了这里的示例,但是我不断收到异常:

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.  

有人能告诉我我做错了什么吗?

i'm trying to create a filter, using fluent nH (1.2) automapping with nH 2.1.2.
I've followed the example here, but I keep getting the exception:

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

the filter class:

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

and in the mapping override:

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

here's my configuration code.

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)));

if I move the line before the automapping part, I get the exception:

 NHibernate.MappingException: filter-def for filter named 'DateFilter' was not found.  

can anybody tell me what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2024-11-09 07:55:03

好的,所以我明白了。当您像这样单独添加映射时,它们最终会出现在不同的映射中,并且它会抱怨您从不使用过滤器,或者抱怨它找不到过滤器,因为它不会在两个地方查找。解决方案是将其直接添加到自动映射中,因此在您的情况下:

//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() 不流畅,但它确实有效。

OK, so I figured this out. When you add the mappings separately like that, they end up in different mappings and it will either complain that you never use the filter or complain that it can't find the filter, because it's not looking both places. The solution is to add it directly to the automap, so in your case like:

//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;
 }));

Kinda gross because .Add() isn't fluent, but it does work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文