0

我正在使用 Entitiy Framework 6.1.3,我想将 N 个表的关系添加到 1 个具有超过 1 个连接列的表。以下示例将帮助您理解我的问题:

具有这些列的表 P(协议表):

protocol_id (int, PK)
message (string)
module (string)
reference_id (int)

protocol_id | module | message | reference_id
-----------------------------------
1 | A | created  | 1
2 | A | modified | 1
2 | A | created  | 2
3 | B | deleted  | 1
4 | B | modified | 1

具有这些列的表 A(模块 A 表):

mod_id (int, PK)
mod_x (string)
mod_y (...)
... 

mod_id | mod_x | ...
--------------------
1 | abc | ...
2 | xyz | ...

包含这些列的表 B(模块 B 表):

mod_id (int, PK)
mod_x (string)
mod_y (...)
... 

mod_id | mod_x | ...
--------------------
1 | abc | ...
2 | xyz | ...

我想在模块 A 中有一个导航属性来获取该行的所有协议 - 像这样:

where module = 'A' and reference_id = mod_id

(同样适用于模块 B、模块 C ...)

在 Java/Hibernate 中,我知道您可以使用多个 ElementJoinColumns(使用 referenceColumnsNames,...)。

如何使用 EF6.1 处理这个问题?谢谢,马库斯。

4

1 回答 1

0

这是DDL。设置正确的参照完整性 (RI) 非常重要:

USE [Breaz]
GO
/****** Object:  Table [dbo].[TableA]    Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableA](
    [mod_id] [int] IDENTITY(1,1) NOT NULL,
    [mod_x] [varchar](10) NULL,
 CONSTRAINT [PK_TableA_] PRIMARY KEY CLUSTERED 
(
    [mod_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[TableB]    Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableB](
    [mod_id] [int] IDENTITY(1,1) NOT NULL,
    [mod_x] [varchar](10) NULL,
 CONSTRAINT [PK_tableb] PRIMARY KEY CLUSTERED 
(
    [mod_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[TableP]    Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableP](
    [protocol_id] [int] IDENTITY(1,1) NOT NULL,
    [message] [varchar](10) NULL,
    [module] [varchar](10) NULL,
    [reference_id] [int] NULL,
 CONSTRAINT [PK_TableP] PRIMARY KEY CLUSTERED 
(
    [protocol_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[TableP]  WITH CHECK ADD  CONSTRAINT [FK_TableP_tablea] FOREIGN KEY([reference_id])
REFERENCES [dbo].[TableA] ([mod_id])
GO
ALTER TABLE [dbo].[TableP] CHECK CONSTRAINT [FK_TableP_tablea]
GO
ALTER TABLE [dbo].[TableP]  WITH CHECK ADD  CONSTRAINT [FK_TableP_tableb] FOREIGN KEY([reference_id])
REFERENCES [dbo].[TableB] ([mod_id])
GO
ALTER TABLE [dbo].[TableP] CHECK CONSTRAINT [FK_TableP_tableb]
GO

现在为三个表添加 edmx,您的 linq 应该如下所示(使用您的 dbcontext 而不是 BreazEntities22 :

       var tableA = e.TableAs.
            Where(w => w.TablePs.Any(s => s.module == "A")).ToList();

        var tableB = e.TableBs.
            Where(w => w.TablePs.Any(s => s.module == "A")).ToList();
于 2016-09-14T22:28:35.357 回答