简体   繁体   English

NHibernate-如何将具有多个外键的表映射到其对应的类?

[英]NHibernate - How to map a table with multiple foreign keys to its corresponding classes?

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: 我创建了以下实体:

  1. User 用户
  2. Project 项目
  3. 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 ? 我如何使用NHibernate映射一个User实体,以便我可以轻松获取他分配给每个项目的访问权限?

I was thinking of doing the following: 我正在考虑执行以下操作:

  1. Create a new entity called "ProjectRight" which will have a Project ID as the primary key 创建一个名为“ ProjectRight”的新实体,该实体将有一个Project ID作为主键
  2. 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..? 如果是,这是否意味着我需要创建两个其他实体,以便可以映射Projects和AccessRights表。

Thanks 谢谢

I'd suggest creating ProjectRight as a component instead of an entity: 我建议将ProjectRight创建为组件而不是实体:

<set name="ProjectRights" table="Users_Projects_Rights">
    <key column="id_User"/>
    <composite-element class="ProjectRight">
        <many-to-one name="Project" column="id_Project"/>
        <many-to-one name="Right" column="id_AccessRight"/>
    </composite-element>
</set>

This is one of the two ways suggested by the NHibernate documentation. 这是NHibernate文档建议的两种方法之一。 For the other one see here . 另一个请看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM