Friday, April 29, 2011

Mapping a ternary association with FluentNHibernate, using IDictionary<TKey,TValue>

I'm trying to map a ternary association using FluentNhibernate.

I have 3 tables:

  • TUser(PK int id, string name, ...)
  • TGroup(PK int id, string name, ...)
  • TRole(PK int id, string name, ...)

And a 4th one that associates them, representing the ternary association:

  • TUserGroupRole(FK int userid, FK int groupid, FK int roleid)

Basically, a user has a particular role for a group. I'm using 3 types in my model :

  • User
  • UserGroup
  • UserRole

In the User class, I want to use an IDictionary<UserGroup,UserRole> to index user roles by groups:

public class User
{
    public virtual int Id { get; set; }
    public virtual IDictionary<UserGroup, UserRole> Roles { get; set; }
    // ...
}

Using regular hbm fashioned XML mapping file, I achieved that with a <map> element, like so (pseudo mapping):

<map name="Roles" table="TUserGroupRole">
    <key column="userid"/>
    <index-many-to-many column="groupid" class="UserGroup"/>
    <many-to-many column="roleid" class="UserRole"/>
</map>

I wasn't able to figure out how to generate the same mapping using FluentNhibernate, so any help on this point would be very appreciated.

From stackoverflow
  • In response to the reply you got on the FNH group, make sure you are exporting your mapping files like:

    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyObject>()
        .ExportTo("path")
    

    I tend to agree with that response because I have only ever seen that type of exception when elements are out of order; that is when the key/index or id element is not the first child.

  • You map collection as IDictionary<UserGroup, UserRole< , but how to map collection as IDictionary<string, UserRole< , where key (of type string) is UserGroup.name?

0 comments:

Post a Comment