views:

15

answers:

1

I'm having some trouble figuring out the correct syntax for mapping a many-to-many relationship with FluentNHibernate. I've looked at several of the other questions here on SO and other places, but haven't seen anything with, specifically, the same table structure. Hoping someone that knows more than I do about FNH can help me figure this out. Here's my table structures:

CREATE TABLE [dbo].[WorkItems](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [CategoryId] [bigint] NOT NULL,
    [DateTime] [datetime] NOT NULL,
    [Details] [nvarchar](2000) NULL,
    [Duration] [int] NOT NULL,
    [DurationInterval] [nvarchar](10) NOT NULL,
    [Summary] [nvarchar](150) NOT NULL,
    [UserId] [bigint] NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)

CREATE TABLE [dbo].[Tags](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)

CREATE TABLE [dbo].[TagWorkItems](
    [TagId] [bigint] NOT NULL,
    [WorkItemId] [bigint] NOT NULL,
 CONSTRAINT [TagId_WorkItemId_PK] PRIMARY KEY CLUSTERED 
(
    [TagId] ASC,
    [WorkItemId] ASC
)

TagId and WorkItemId in the TagWorkItems table are both foreign keys back to the parent tables. It's a pretty straight-forward join table setup for a m2m relationship. My Tag class has a property of type ICollection, and my WorkItem class has a property of ICollection. I can't seem to figure out how to setup the Mappings for those properties. Any advice would be greatly appreciated. Thanks.

A: 

Here's an example of what a WorkItem mapping might say; the readonly bit depends on how you're going to maintain the relationship:

HasManyToMany(x => x.Tags)
            .Table("TagWorkItems")
            .ParentKeyColumn("WorkItemId")
            .ChildKeyColumn("TagId")
            .AsSet()
            .ReadOnly();
Tahbaza
That seems to have given me what I needed and works. Thanks very much. Much appreciated.
Bob Yexley

related questions