本文介绍了属性过滤与流利的nHibernate自动映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个过滤器,使用流畅的nH(1.2)自动映射与nH 2.1.2。

我已经按照示例,但我不断收到异常:

 过滤器名为'DateFilter'的filter-def从来没有用于过滤类和集合.. 

过滤器类:

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

这里是我的配置代码。

 流利的.Configure()
.Database(MsSqlConfiguration.MsSql2008
.DefaultSchema(dbo) //设置默认模式以启用全限定查询
.AdoNetBatchSize(batchSize> 0?batchSize:1)
.UseReflectionOptimizer()
.ConnectionString(c => c.FromConnectionStringWithKey )在Web环境中,我们可以使用下面的方法来创建一个Web应用程序(例如:connectionStringKey))
.Cache(c => c.UseQueryCache()
.ProviderClass(
isWeb?typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName /使用sysCache2
:typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName //在开发环境中使用stupid cache
))

.Mappings(m => m。 AutoMappings .Add(
AutoMap.AssemblyOf< Domain.Entity>(cfg)// automapping域实体
.IncludeBase< Domain.SomethingBase>()//确保虽然SomethingBase是一个基类,以及。这使得我们可以将所有的Something子类存储在同一个表中
.IncludeBase< Domain.OrOtherBase>()//为抽象的OrOtherBase类创建一个表$ b $ .UseOverridesFromAssemblyOf< MappingOverrides.MappingOverride> ()
.Conventions.Add(DefaultCascade.All())//确保所有的保存级联(即当我们保存一个区域,它的队列也保存)
.Conventions.AddFromAssemblyOf< IdGenerationWithHiLoConvention>()
))
.Mappings(m => m.FluentMappings.Add(typeof(DateFilter)));

如果我在automapping部分之前移动行,我得到异常:

  NHibernate.MappingException:未找到名为DateFilter的过滤器的filter-def。 

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

解决方案

好的,所以我想通了。当你像这样分别添加映射时,它们会以不同的映射结束,它会抱怨你从不使用过滤器,或者抱怨它找不到过滤器,因为它看起来不是这两个地方。解决的办法是把它直接添加到automap,所以在你的情况下,如:

  //其他的东西在这里
.Mappings(m => m.AutoMappings.Add(()=> {
var a = AutoMap.AssemblyOf< Domain.Entity>(cfg)
.IncludeBase< Domain.SomethingBase>( )//还有级联和约定和东西
a.Add(typeof(DateFilter));
return a;
}));

由于 .Add() isn流利,但确实有效。

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?

解决方案

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.

这篇关于属性过滤与流利的nHibernate自动映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:56