NHibernate.Mapping.Attributes.Filter

杀马特。学长 韩版系。学妹 提交于 2020-01-15 12:13:03

问题


I'm mapping my database tables using NHibernate with NHibernate.Mapping.Attributes library and I got stuck to get the Filter attributes to work.

Suppose a class A that has a set of objects of class B. So, I have, the following:

[NHibernate.Mapping.Attributes.Set(0, Inverse = true, Lazy = NHibernate.Mapping.Attributes.CollectionLazy.False)]
[NHibernate.Mapping.Attributes.Key(1, Column = "ClassAId")]
[NHibernate.Mapping.Attributes.OneToMany(2, Class = "ClassB, Assembly")]

    public virtual ISet<ClassB> ClassBs { get; set; }

I want to create a filter on this collection to bring only class B objects that satisfy a given criteria, such as Status = 1.

How can I create such Filter?


回答1:


The where parameter of the Set mapping should be able help you out. Per the documentation the where parameter:

where: (optional) specify an arbitrary SQL WHERE condition to be used when retrieving or removing the collection (useful if the collection should contain only a subset of the available data)

So to filter on Status (assuming Status is a SQL column in the table mapped for ClassB - though this column does not have to be mapped in the NHibernate mapping).

[NHibernate.Mapping.Attributes.Set(0,...., Where = "Status = 1", .....)]
...
public virtual ISet<ClassB> ClassBs { get; set; }


来源:https://stackoverflow.com/questions/1787074/nhibernate-mapping-attributes-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!