使用 NHibernate 创建虚拟多对多关系
假设您有三个实体 - Categories
、Sites
和 Items
。 Items 表有一个 CategoryID
和一个 SiteID
。如果我想查找给定类别的站点,我可以通过 NHibernate 中的映射来获取,如下所示:
public CategoryMap() { //.. ManyToMany(x => x.Site) .Table("Items") .ParentKey("CategoryID") .ChildKey("SiteID"); }
如果我想要所有类别以及该类别所属站点的列表,这非常有用。但我还想创建一个名为“未分类”的类别,并列出不属于其中类别的网站。我可以使用第二个查询来完成此操作,但我想知道是否有一种方法可以创建一个“虚拟”类别,该类别将成为类别集合的一部分,或者我缺少的其他一些技巧。我也愿意被告知这是非常错误的。
Let's say you have three entities - Categories
, Sites
and Items
. The Items table has a CategoryID
and a SiteID
. If I want to find the sites for a given category, I can get that with a mapping in NHibernate that looks something like:
public CategoryMap() { //.. ManyToMany(x => x.Site) .Table("Items") .ParentKey("CategoryID") .ChildKey("SiteID"); }
This works great if I want a list of all of the categories, and the sites the category belongs to. But I also want to create a category called "Uncategorized" and list the Sites which don't belong to a Category in there. I could do it with a second query, but I was wondering if there was a way to create a "virtual" Category that would be part of the Category collection, or some other trick to this I'm missing. I'm also open to being told that this is highly wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将是您的模型/服务的一部分; NHibernate 无法提供它。
That would be part of your Model / Service; NHibernate can't provide it.