ModelInspector似乎没有提供定义过滤器定义的方法。任何想法/解决方法?如何通过代码添加Nhibernate 3.2映射的过滤器定义?
我需要生成由代码映射如下:
<filter-def name="filterName" use-many-to-one="false">
<filter-param name="filterParamName" type="Int32"/>
</filter-def>
ModelInspector似乎没有提供定义过滤器定义的方法。任何想法/解决方法?如何通过代码添加Nhibernate 3.2映射的过滤器定义?
我需要生成由代码映射如下:
<filter-def name="filterName" use-many-to-one="false">
<filter-param name="filterParamName" type="Int32"/>
</filter-def>
我能够实现,使用NHibernate.Cfg.Configuration:
var cfg = new Configuration();
var filterDef = new FilterDefinition(
"filterName",
null, // or your default condition
new Dictionary<string, IType> { { "filterParamName", NHibernateUtil.Int32 } },
false);
cfg.AddFilterDefinition(filterDef);
// cfg.AddMapping(...)
// cfg.DataBaseIntegration(...)
var sessionFactory = cfg.BuildSessionFactory();
然后定义在实体映射滤波器:
public class EntityMap : ClassMapping<Entity>
{
public EntityMap()
{
Table("Entity");
Filter("filterName", m => m.Condition("FilteredField = :filterParamName"));
// remaining mapping
}
}
然后按如下方式使用它:
using(var session = sessionFactory.OpenSession())
{
var filterValue = 123;
session
.EnableFilter("filterName")
.SetParameter("filterParamName", filterValue);
}
我希望你能找到这个有用的。
仅供参考,
需要注意的是调用AddFilterDefinition
是AddMapping
之前是很重要的,否则你将得到一个ArgumentException("An item with the same key has already been added")
!
这使我的一天! –