nHibernate - 一个仅包含超类型的集合

发布于 2024-10-09 12:03:53 字数 693 浏览 2 评论 0原文


我有以下类:

class Employee
{
    public string Name { get; set; }
}

class HistoricalEmployee : Employee
{
    public DateTime TerminationDate { get; set; }
}

class Company
{
    public IList<Person> CurrentEmployees { get; set; }
}


Employee 和 HistoricalEmployee 使用每类表层次结构策略进行映射。
当我检索 CurrentEmployees 集合时,我希望它只包含 Employee 元素,而不是 HistoricalEmployees 元素。
当员工“死亡”时,他们并没有真正被删除,
但他们变成了 HistoricalEmployee(具有更多属性,例如终止日期等)。
显然,随着时间的推移,HistoricalEmployees 的数量将超过Employees 的数量,
因此,当我只需要当前的Employees 时,我无法获取所有HistoricalEmployees。

如何(流畅地)将集合配置为仅检索超类的元素?
我认为这与多态性属性有关,但我无法真正弄清楚如何做到这一点。


谢谢,
强尼

I have the following classes:

class Employee
{
    public string Name { get; set; }
}

class HistoricalEmployee : Employee
{
    public DateTime TerminationDate { get; set; }
}

class Company
{
    public IList<Person> CurrentEmployees { get; set; }
}

Employee and HistoricalEmployee are mapped using table-per-class-heirarchy strategy.

When I retrieve the CurrentEmployees collection, I want it only to contain elements that are Employee, and NOT HistoricalEmployees.
when an employee 'dies', they're not really deleted,
but they become HistoricalEmployee (with a few more attributes, such as termination date etc.).
Obviously, over time, the number of HistoricalEmployees will exceed the number of Employees by magnitudes,
so I can't fetch all HistoricalEmployees when I only need current Employees.

How can I (fluently) configure the collection to only retrieve elements of the super class?
I think it's something to do with the Polymorphism property, but I couldn't really figure out how to do that.

thanks,
Jhonny

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-10-16 12:03:53

好的,我这样做了:

mapping.HasMany(x => x.CurrentEmployees)
            //.Where(pqa => pqa.TimeOut != null)
            .Where("TerminationDate is null")

显然,.Where() 函数在属性上创建了一个过滤器,这正是我所需要的。
请注意,我使用了字符串版本,并注释掉了 Func<>版本。
这是因为据我所知,目前(FNH 1.1)Func<>版本不起作用。
希望这对某人有帮助,
J

Ok, I did this like so:

mapping.HasMany(x => x.CurrentEmployees)
            //.Where(pqa => pqa.TimeOut != null)
            .Where("TerminationDate is null")

apparently, the .Where() function creates a filter on the property, which is exactly what I needed.
notice that I used the string version, and commented-out the Func<> version.
This is because that currently (FNH 1.1), as far as I could determine, the Func<> version doesn't work.
hopes this helps somebody,
J

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文