Using NHibernate, how can I map the following scenario:
public class User
{
public virtual IDictionary<Project, IList<Role>> ProjectAssignments { get; set; }
}
In the database I have separate tables for User, Project and Role and a fourth table for the relationship.
Any help would be appreciated.
You can’t – from Ayende’s blog discussing <map>
You’ll need some intermediary entity/component. I’d probably have a ProjectAssignment entity to break up the nary association between User, Project and Role – it will probably grow extra attributes as time goes on (say you want to track changes to the assignments over time, so it gets
StartDateandEndDateproperties).Something like:
I’d just map the ProjectAssignment as a persistent class in its own right, and the User.Assignments collections as a normal one-to-many of whatever flavour. Finally, I’d add a method to extract the details into a dictionary (or probably an ILookup if you’re using Framework v3.5): something like: