codecompleting
codecompleting

Reputation: 9611

Mapping using an intermediate join table with 2 primary keys

How can I fluently map 2 entities that are joined together using a "join" table (it has 2 primary keys).

My entities:

Users
Roles

Then the 'join' table looks like:

RolesUsers
  RoleId
  UserId

So I want to query like this:

user.Roles

Upvotes: 0

Views: 112

Answers (1)

Cole W
Cole W

Reputation: 15293

In Fluent NHibernate this is achieved by using HasManyToMany in your mapping. Ex: (User mapping)

 HasManyToMany(x => x.Roles)
                .Table("RolesUsers")
                .ParentKeyColumn("UserId")
                .ChildKeyColumn("RoleId")
                .Cascade.All()
                .Inverse()

Upvotes: 1

Related Questions