Saving a many to many with extra information
I have a UserRole class which contains a list of enums. I can save instances of UserRole along with this list using the following mapping:
HasMany(x => x.Functions)
.Table("RoleFunctions")
.KeyColumn("RoleId")
.Element("FunctionId")
.AsSet()
.Not.LazyLoad();
I would now like to add a list of pairs containing another of my domain classes and an associated permission integer, e.g. Tuple<DomainClass, int>, and still be able to just save an instance of the UserRole class. I already have mappings for the other domain classes but I'm not sure how to configure the UserRole mapping to do this.
Can anyone help?
Support Staff 2 Posted by Paul Batum on 23 Sep, 2010 12:08 AM
I had a bit of trouble visualizing what you are trying to do. It might be
easier if you provide the classes you want mapped (doesnt have to be the
full class, just an abbreviated version with the relevant properties would
be best) and a brief explanation of the schema you want to map it too.
On Mon, Sep 20, 2010 at 5:01 AM, Andrew Hepburn <
tender+d1b1c6639f44f647b03ee2c390147198a9394cbad@tenderapp.com<tender%2Bd1b1c6639f44f647b03ee2c390147198a9394cbad@tenderapp.com>
> wrote:
3 Posted by Andrew Hepburn on 23 Sep, 2010 03:48 PM
Thanks for responding Paul!
Here's a simplified version of my UserRole class
public class Role
{
public Name { get;set; }
public ICollection<Function> Functions { get; set; }
}
I would like to add a further public property to this class. Something along the lines of
public ICollection<Tuple<MessageType, int> MessageTypePermissions { get; set; }
This would allow me to check each role to see what permissions each role has to access different instances the MessageType class. For example, this might allow read-only access to emails, but send privileges on SMS messages.
I currently have a two tables:
UserRole
{
Id int,
Name varchar(50)
}
UserRoleFunctions
{
RoleId int,
FunctionId int
}
This part works well but I guess I now need another table along the lines of:
UserRoleMessageTypes
{
RoleId int,
MessageTypeId int,
Permissions int
}
If you can suggest how to update the mapping to handle the MessageTypes part I would be very grateful.
Support Staff 4 Posted by Paul Batum on 25 Sep, 2010 10:14 PM
Rather than using a collection of Tuple's which I'm not sure if NH supports,
how about a dictionary?
public class Role
{
public Name { get;set; }
public ICollection<Function> Functions { get; set; }
public IDictionary<MessageType, int> MessageTypePermissions { get; set;
}
}
Now the way you would map this has changed recently, and I'm not across the
changes yet. Hopefully it is as simple as HasMany(x =>
x.MessageTypePermissions) but I could be wrong - give it a go and let us
know if you're stuck.
On Thu, Sep 23, 2010 at 10:50 AM, Andrew Hepburn <
tender+d1b1c6639f44f647b03ee2c390147198a9394cbad@tenderapp.com<tender%2Bd1b1c6639f44f647b03ee2c390147198a9394cbad@tenderapp.com>
> wrote: