FluentNHibernate HasManyToMany syntax
CREATE TABLE [dbo].[User](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) NOT NULL,
[Password] [varchar](50) NOT NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[UserID] ASC
) ON [PRIMARY]
CREATE TABLE [dbo].[Module](
[ModuleID] [int] NOT NULL,
[ModuleName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Module] PRIMARY KEY CLUSTERED
(
[ModuleID] ASC
) ON [PRIMARY]
CREATE TABLE [dbo].[Role](
[RoleID] [int] NOT NULL,
[RoleName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED
(
[RoleID] ASC
) ON [PRIMARY]
CREATE TABLE [dbo].[UserRoleSetting](
[UserID] [int] NOT NULL, /* FK to User table */
[ModuleID] [int] NOT NULL, /* FK to Module table */
[RoleID] [int] NOT NULL, /* FK to Role t开发者_如何学Goable */
CONSTRAINT [PK_UserRoleSetting] PRIMARY KEY CLUSTERED
(
[UserID] ASC,
[ModuleID] ASC
) ON [PRIMARY]
GO
I have a schema like this to define the users have different roles under different modules. I know if UserRoleSetting table is just a simple many to many relationship table, it is easy to define. But that table actually contains relationship from 3 different tables, so what might be the correct syntax to load user role settings into user object?
Thanks
Hardy
What you would like to map is a three-way many-to-many.
You can't define it directly. You instead create a separate entity UserRoleSetting and map it using: one-to-manys in User, Module, Role and many-to-one in UserRoleSetting. In FluentNHibernate it translates into HasMany() and References() respectively.
You can omit one side of the mapping of each connection if bi-directionality is not needed.
Does that solve your problem?
精彩评论