ModelInspector似乎没有提供定义过滤器定义的方法。任何想法/解决方法?
我需要通过代码生成以下映射:
<filter-def name="filterName" use-many-to-one="false">
<filter-param name="filterParamName" type="Int32"/>
</filter-def>
答案 0 :(得分:9)
我能够使用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);
}
我希望你;我觉得这很有用。
答案 1 :(得分:6)
FYI,
请务必注意,对AddFilterDefinition
的调用是AddMapping
之前,否则您将获得ArgumentException("An item with the same key has already been added")
!