实体框架特定列上的多对多关系

发布于 2024-12-22 13:49:11 字数 1388 浏览 2 评论 0原文

我有 3 个表:书籍、章节和内容。我想在部分和内容之间添加多对多关系。部分和内容表有一个 PageNo 列。一个页面可能有很多内容和很多部分。简而言之:

Book 1----* Section (on BookId)
Book 1----* Content (on BookId)
Section *-----* Content (on PageNo)

PageNo 对于部分表和内容表来说都不是唯一的。所以我无法在Sql Server中为PageNo添加外键。

我尝试创建一个像这样的联结表:

SectionContent: [SectionId, ContentId]

并且我为此联结表添加了 FK。因此实体框架可以理解联结表,并在SectionId和ContentId上建立多对多关系。但是每次当我需要插入节或内容表之一时,我也必须插入到节内容连接表中。因此,首先我必须检查连接表中是否已有相同的记录。项目中还有很多插入操作。我必须搜索所有插入操作,并且必须添加额外的查询以插入到连接表中。

我还需要获取页面中的部分和内容。这对我来说是额外的努力。

我可以删除部分表和内容表之间的关系。我可以在 PageNo 列上使用额外的联接查询。但我想使用实体。我想以像Section.Contents这样的实体方式获取Contents,并且我想以像Content.Sections那样的相同方式获取Sections。

那么我可以在没有 SQL Server 的 FK 的情况下在 PageNo 列上的部分和内容之间添加多对多关联吗?

编辑:另外,如果我使用上面的联结表,我必须执行这样的sql查询,是吗?

INSERT INTO SectionContent
SELECT * FROM 
(
    SELECT Section.id AS SectionId, Content.id AS ContentId
    FROM Section
    LEFT OUTER JOIN Content
        ON Section.PageNo = Content.PageNo AND 
           Section.BookId = Content.BookId 

    UNION

    SELECT Section.id AS SectionId, Content.id AS ContentId
    FROM Section
    RIGHT OUTER JOIN Content
        ON Section.PageNo = Content.PageNo AND 
           Section.BookId = Content.BookId 
) AS T
WHERE SectionId is not NULL AND ContentID is not NULL
GROUP BY T.SectionId, T.ContentId

I have 3 tables: Book, Section and Content. I want to add many-to-many relation between Section and Content. Section and Content tables have a PageNo column. A Page may have many contents and many Sections. In Brief:

Book 1----* Section (on BookId)
Book 1----* Content (on BookId)
Section *-----* Content (on PageNo)

The PageNo is not unique for both Section and Content tables. So I can't add foreign key for PageNo in Sql Server.

I tried to create a junction table like this:

SectionContent: [SectionId, ContentId]

And I added FKs for this junction table. So entity framework could understand the junction table and it set up many-to-many relationship on SectionId and ContentId. But everytime when I need to insert one of Section or Content Table, I have to insert to SectionContent junction table, too. So first I have to check if there is a same record already in the junction table. Also there are a lot of insert operations in the project. I have to search for all insert operations and I have to add extra query to insert into the junction table.

Also I need get the sections and contents in a page. This is extra effort for me.

I can remove the relationship between Section and Content tables. And I can use extra join queries on PageNo column. But I want to use entity. I want to get Contents in entity way like Section.Contents and I want to get Sections in the same way like Content.Sections.

So can I add many-to-many association between Section and Content on PageNo column without SQL Server's FKs?

Edit: Also If I use the junction table above, I must execute an sql query like this, do I?

INSERT INTO SectionContent
SELECT * FROM 
(
    SELECT Section.id AS SectionId, Content.id AS ContentId
    FROM Section
    LEFT OUTER JOIN Content
        ON Section.PageNo = Content.PageNo AND 
           Section.BookId = Content.BookId 

    UNION

    SELECT Section.id AS SectionId, Content.id AS ContentId
    FROM Section
    RIGHT OUTER JOIN Content
        ON Section.PageNo = Content.PageNo AND 
           Section.BookId = Content.BookId 
) AS T
WHERE SectionId is not NULL AND ContentID is not NULL
GROUP BY T.SectionId, T.ContentId

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

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

发布评论

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

评论(1

小女人ら 2024-12-29 13:49:11

我通过使用 EF Code First 解决了这个问题。我已将所有表实现为一个类,并且 EF 处理所有多对多关系。

I solved this issue by using EF Code First. I've implemented all tables as a class and EF handled all many-to-many relationships.

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