FluentNHibernate HasManyToMany 语法

发布于 2024-09-14 23:40:06 字数 1041 浏览 4 评论 0原文

我在找出与 FluentNHibernate 映射多对多关系的正确语法时遇到了一些麻烦。我已经查看了 SO 和其他地方的其他几个问题,但没有看到任何具有相同表结构的内容。希望比我更了解 FNH 的人可以帮助我解决这个问题。这是我的表结构:

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
)

TagWorkItems 表中的 TagId 和 WorkItemId 都是返回父表的外键。对于 M2M 关系来说,这是一个非常简单的连接表设置。我的 Tag 类有一个 ICollection 类型的属性,我的 WorkItem 类有一个 ICollection 类型的属性。我似乎无法弄清楚如何为这些属性设置映射。任何建议将不胜感激。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

悲欢浪云 2024-09-21 23:40:06

以下是 WorkItem 映射可能表示的内容的示例;只读位取决于您将如何维护关系:

HasManyToMany(x => x.Tags)
            .Table("TagWorkItems")
            .ParentKeyColumn("WorkItemId")
            .ChildKeyColumn("TagId")
            .AsSet()
            .ReadOnly();

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