这是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();