NHibernate 映射 - 通过表进行多对一
我正在使用具有以下结构的现有数据库。更改数据库架构是最后的手段。
Products
Id
Name
ParentProducts
ParentId
ChildId
我不需要 ParentProducts 的实体,我有以下子属性(仍然需要测试它,但这就是概念)。
<bag name="Children" lazy="true" table="dbo.ParentProducts" cascade="save-update" inverse="true" >
<key column="[ChildId]"></key>
<many-to-many column="[ProductId]" class="Product" />
</bag>
我所困扰的是如何创建父属性?我想做类似以下的事情,但 table 不是多对一的有效属性。
<many-to-one name="Parent" column="[ParentId]" table="dbo.ParentRelated" class="Policy" />
我可以创建一个包,然后只查看第一个项目,但这更像是一种黑客行为。
有什么想法吗? 谢谢
I'm working with an existing database that has the following structure. Changing the database schema is a last resort.
Products
Id
Name
ParentProducts
ParentId
ChildId
I don't want an entity for ParentProducts, I have the following for the children property (still need to test it, but that's the concept).
<bag name="Children" lazy="true" table="dbo.ParentProducts" cascade="save-update" inverse="true" >
<key column="[ChildId]"></key>
<many-to-many column="[ProductId]" class="Product" />
</bag>
What I'm struggling with is how do I create a Parent property? I'd like to do something like the following, but table isn't a valid attribute for many-to-one.
<many-to-one name="Parent" column="[ParentId]" table="dbo.ParentRelated" class="Policy" />
I could create a bag and only ever look at the first item, but that's more of a hack.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个袋子是最简单的解决方案。您可以给它一个干净的接口:
这样,其余代码就不需要了解数据库/映射结构。
Creating a bag is the easiest solution. And you can give it a clean interface:
With this, the rest of the code doesn't need to be aware of the DB/mapping structure.