简体   繁体   English

NHibernate-多表映射?

[英]NHibernate - Multiple table mapping?

I'm working on a project which handles Users and Projects: 我正在处理一个处理用户和项目的项目:

  • A user may be assigned to zero or X projects. 可以将一个用户分配给零个或X个项目。
  • A user has read or write rights on a project. 用户对项目具有读取或写入权限。

In my database, I have a table named Users_Projects_Rights, which is used to associate a user to a certain number of projects, as well as specify their access rights on them (it's simply a boolean value, true for write rights, false for read rights). 在我的数据库中,我有一个名为Users_Projects_Rights的表,该表用于将用户与一定数量的项目相关联,并指定其对他们的访问权限(这只是一个布尔值,写权限为true,读权限为false )。

The Users_Projects_Rights table columns are: Users_Projects_Rights表列为:

  • id_User id_User
  • id_Project id_Project
  • CanWrite (boolean) CanWrite(布尔值)

My User class currently has this collections: 我的User类当前具有以下集合:

    /// <summary>
    /// Gets or sets a user's accessible projects, with the value being 
    /// the name of the project.
    /// </summary>
    public virtual IDictionary<int, string> AssignedProjects { get; set; }

Taking in consideration that I have a Projects table, which has a Name column: 考虑到我有一个Projects表,其中有一个Name列:

  • How can I map my AssignedProjects dictionnary, in my User class, in order to have the project Id as the key (using a ternary mapping with the Users_Projects_Rights table) and the project name (taken from the Projects table) as the value ? 我如何在User类中映射我的AssignedProjects字典,以便将项目ID作为键(使用具有Users_Projects_Rights表的三元映射)和项目名称(从Projects表中获取)作为值?

Here is what I came up with, but I have no idea how to map the value of the dictionary: 这是我想出的,但是我不知道如何映射字典的值:

<map name="AssignedProjects" table="Users_Projects_Rights">
  <key column="id_User"/>
  <map-key column="id_Project" type="Int32"/>
  (How do I map the value... ?)
</map>

Your design is wrong.Because a user can have many project so 您的设计是错误的。因为用户可以有很多项目,所以

public virtual IDictionary AssignedProjects { get; 公共虚拟IDictionary AssignedProjects {get; set; 组; } }

will throw key already added exception if mapped.So design as user have list of projects which are assigned to him 如果被映射,将抛出已添加的密钥异常。因此根据用户的设计有分配给他的项目列表

public virtual List AssignedProjects { get; 公共虚拟列表AssignedProjects {get; set; 组; } }

I got it, if someone has a more efficient answer please let me know: 我明白了,如果有人有更有效的答案,请告诉我:

<map name="AssignedProjects" table="Users_Projects_Rights">
  <key column="id_User"/>
  <index column="id_Project" type="System.Int32"/>
  <element formula="(SELECT Projects.name FROM Projects WHERE Projects.id = id_Project)"/>
</map>

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

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