0
votes

I'm using Entitiy Framework 6.1.3 and I want to add a relation from N tables to 1 table with more than 1 join columns. The following example will help you to understand my problem:

Table P (protocol table) with these columns:

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

Table A (module A table) with these columns:

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

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

Table B (module B table) with these columns:

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

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

I would like to have a navigation property in module A to get all procotols for this row - sthg like that:

where module = 'A' and reference_id = mod_id

(same for module B, module C ...)

In Java/Hibernate, I know you can use more than one ElementJoinColumns (with referenceColumnsNames, ...).

How can I handle this with EF6.1? Thanks, Markus.

1

1 Answers

0
votes

Here is the DDL. It is important that you have the proper referential integrity (RI) set up:

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

Now add the edmx for the three tables, and your linq should look like this (use your dbcontext instead of 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();