0

I'm working on a project which handles Users and Projects:

  • A user may be assigned to zero or X projects.
  • 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).

The Users_Projects_Rights table columns are:

  • id_User
  • id_Project
  • CanWrite (boolean)

My User class currently has this collections:

    /// <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:

  • 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 ?

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>
Hussein Khalil
  • 1,585
  • 2
  • 25
  • 47

2 Answers2

0

Your design is wrong.Because a user can have many project so

public virtual 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; set; }

Anand
  • 717
  • 6
  • 20
0

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>
Hussein Khalil
  • 1,585
  • 2
  • 25
  • 47