FluentNHibernate HasManyToMany 语法
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 table */
CONSTRAINT [PK_UserRoleSetting] PRIMARY KEY CLUSTERED
(
[UserID] ASC,
[ModuleID] ASC
) ON [PRIMARY]
GO
我有一个这样的模式来定义用户在不同模块下具有不同的角色。我知道如果 UserRoleSetting 表只是一个简单的多对多关系表,那么很容易定义。但该表实际上包含来自 3 个不同表的关系,那么将用户角色设置加载到用户对象中的正确语法可能是什么?
谢谢哈迪
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 table */
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要映射的是三路多对多。
你不能直接定义它。相反,您可以创建一个单独的实体 UserRoleSetting 并使用以下方式映射它:用户、模块、角色中的一对多以及 UserRoleSetting 中的多对一。在 FluentNHibernate 中,它分别转换为 HasMany() 和 References()。
如果不需要双向性,您可以省略每个连接映射的一侧。
这能解决你的问题吗?
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?