views:

46

answers:

1

I have tables like this:

tblUsers
int UserID
string UserName


tblUsersInRoles
int UserID
int RoleID

tbleRoles
int RoleID
string RoleName

A user can be in many roles.

Is there a way to create my mapping file for my users so that my users object contains a list of Roles? I've figured out to do it so that I have a list of RoleID's like this:

<bag name="Roles" table="tblUsersInRoles" cascade="all">
  <key column="UserId"/>
  <one-to-many class="UsersInRoles"/>
</bag>

Is there a way to take that the next step via the mapping file and store the roles themselves instead of just their ID's?

Thanks for any help

+1  A: 

You dont need UsersInRoles, to do it you can use this mapping:

<bag name="Roles" table="tblUsersInRoles" cascade="none"> 
  <key column="UserId"></key> 
  <many-to-many class="Role" column="RoleId"></many-to-many> 
</bag>

And you can read this post about many to many. And about cascading - if you set cascade to all then deleting of user will couse deleting of his roles - I don't think you need it.

Sly
Awesome, worked a charm. Thanks heaps
Adam