gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:32:12 25 4
gpt4 key购买 nike

我正在尝试创建一个过滤器,使用流畅的 nH (1.2) 自动映射和 nH 2.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.  

谁能告诉我我做错了什么?

最佳答案

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

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

关于nhibernate - 具有流畅 nHibernate 自动映射的属性过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5728935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com