FluentNHibernate HasManyToMany 语法

发布于 2024-10-19 03:29:53 字数 1080 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-10-26 03:29:53

您想要映射的是三路多对多。

你不能直接定义它。相反,您可以创建一个单独的实体 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文