NHibernate - 如何将具有多个外键的表映射到其相应的类?
我有下表:
基本上,用户是项目的一部分,并且对该项目具有一定的访问权限特定项目。
我创建了以下实体:
- 用户
- 项目
- AccessRight
如何使用 NHibernate 映射用户实体,以便我可以轻松获取他分配给的每个项目的访问权限?
我正在考虑执行以下操作:
- 创建一个名为“ProjectRight”的新实体,该实体将项目 ID 作为主键
在用户实体中创建一个“多对多”集:
公共虚拟 ICollection
;项目权利{获取;放; } 并在用户映射中:
<键列=“id_UserGroup”> <多对多类=“ProjectRight”> <列名=“id_Project”/> <列名=“id_AccessRight”/>
这行得通吗?如果是,这是否意味着我需要创建两个额外的实体,以便我可以映射 Projects 和 AccessRights 表..?
谢谢
I have the following tables :
Basically, a user is part of a project and has certain access rights on that particular project.
I created the following entities:
- User
- Project
- AccessRight
How can I map, using NHibernate, a User entity so that I can easily fetch the access rights per project he's assigned to ?
I was thinking of doing the following:
- Create a new entity called "ProjectRight" which will have a Project ID as the primary key
Create a "Many-To-Many" set within the User entity :
public virtual ICollection<ProjectRight> ProjectRights { get; set; }
And in the User mapping:
<set name="ProjectRights" table="Users_Projects_Rights"> <key column="id_UserGroup"></key> <many-to-many class="ProjectRight" > <column name="id_Project" /> <column name="id_AccessRight" /> </many-to-many> </set>
Would this work ? And if yes, does that mean that I'll need to create two additional entities so that I can map the Projects and AccessRights table..?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议将
ProjectRight
创建为组件而不是实体:这是 NHibernate 文档建议的两种方法之一。对于另一个,请参阅此处。
I'd suggest creating
ProjectRight
as a component instead of an entity:This is one of the two ways suggested by the NHibernate documentation. For the other one see here.