我如何在nHibernate中映射这个?

时间:2022-09-08 11:27:57

Trying to wrap my head around nHibernate, curiuos how this scenerio would be handled:

试图围绕nHibernate,围绕如何处理这个场景:

Post (postID, title, content, dateCreated)
Category (categoryID, name, postCount)

post_to_categories (postID, categoryID)

If I create a Post, it should insert into Post, insert into post_to_categories and update the postCount in Category.

如果我创建一个Post,它应该插入Post,插入post_to_categories并更新Category中的postCount。

I am planning on using the iRepository pattern also.

我也计划使用iRepository模式。

How would this be done with nHibernate? (mapping file example?)

如何用nHibernate完成这项工作? (映射文件示例?)

1 个解决方案

#1


0  

The "hard" part of this seems to be updating the post count in the category table... but why update it? You can just define the value like this (inside the mapping file for Category):

这个“硬”部分似乎是更新类别表中的帖子数...但为什么要更新呢?您可以像这样定义值(在Category的映射文件中):

<property name="PostCount" formula="select count(*) from Posts p where p.Id = Id" />

If your post-to-category is many to many, it should be possible to extend the above to support that as well.

如果你的后期类别是多对多,那么应该可以扩展上述内容以支持它。

#1


0  

The "hard" part of this seems to be updating the post count in the category table... but why update it? You can just define the value like this (inside the mapping file for Category):

这个“硬”部分似乎是更新类别表中的帖子数...但为什么要更新呢?您可以像这样定义值(在Category的映射文件中):

<property name="PostCount" formula="select count(*) from Posts p where p.Id = Id" />

If your post-to-category is many to many, it should be possible to extend the above to support that as well.

如果你的后期类别是多对多,那么应该可以扩展上述内容以支持它。