简体   繁体   English

实体框架未建立关联并显示联结表

[英]Entity Framework not making association and displays junction tables

We're trying to setup a new database and application using database first entity framework. 我们正在尝试使用数据库优先实体框架设置新的数据库和应用程序。

Everything is working well except for this bunch of tables. 除了这堆桌子,其他所有东西都运行良好。 The entity framework isn't setup an association and is instead showing the junction tables in 2 of the 3 instances. 实体框架未设置关联,而是在3个实例中的2个中显示联结表。

In the picture, ProfileIntegrationToolkit and ProfileProviderPrimary should disapear and should instead be associations. 在图中,ProfileIntegrationToolkit和ProfileProviderPrimary应该消失,而应该是关联。

实体框架图

Here's the code to reproduce the tables 这是复制表格的代码

/****** Object:  Table [dbo].[AuthorizedType]    Script Date: 25/02/2016 3:04:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AuthorizedType](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [NameEng] [nvarchar](250) NOT NULL,
    [NameFra] [nvarchar](250) NOT NULL,
    [DescriptionEng] [nvarchar](max) NULL,
    [DescriptionFra] [nvarchar](max) NULL,
 CONSTRAINT [PK_AuthorizedToOperateTypes] PRIMARY KEY CLUSTERED 
(
    [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] TEXTIMAGE_ON [PRIMARY]

GO
/****** Object:  Table [dbo].[IntegrationToolkit]    Script Date: 25/02/2016 3:04:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[IntegrationToolkit](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [NameEng] [nvarchar](250) NOT NULL,
    [NameFra] [nvarchar](250) NOT NULL,
    [DescriptionEng] [nvarchar](max) NULL,
    [DescriptionFra] [nvarchar](max) NULL,
 CONSTRAINT [PK_IntegrationToolkits] PRIMARY KEY CLUSTERED 
(
    [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] TEXTIMAGE_ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Profile]    Script Date: 25/02/2016 3:04:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Profile](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [AuthorizedToOperateTypeId] [int] NOT NULL,
    [AuthorizationDate] [datetime2](7) NOT NULL,
    [ProviderPrimaryId] [int] NOT NULL,
    [ProviderSecondaryId] [int] NOT NULL,
    [IntegrationToolkitId] [int] NOT NULL,
 CONSTRAINT [PK_CyberSecurityProfile] PRIMARY KEY CLUSTERED 
(
    [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
/****** Object:  Table [dbo].[ProfileIntegrationToolKit]    Script Date: 25/02/2016 3:04:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProfileIntegrationToolKit](
    [ProfileId] [int] NOT NULL,
    [IntegrationToolkitId] [int] NOT NULL,
 CONSTRAINT [PK_CyberSecurityProfileIntegrationToolKitOther] PRIMARY KEY CLUSTERED 
(
    [ProfileId] ASC,
    [IntegrationToolkitId] 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
/****** Object:  Table [dbo].[ProfileProviderPrimary]    Script Date: 25/02/2016 3:04:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProfileProviderPrimary](
    [ProfileId] [int] NOT NULL,
    [ProviderId] [int] NOT NULL,
 CONSTRAINT [PK_CyberSecurityProfileCredentialProviderPrimary] PRIMARY KEY CLUSTERED 
(
    [ProfileId] ASC,
    [ProviderId] 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
/****** Object:  Table [dbo].[ProfileProviderSecondary]    Script Date: 25/02/2016 3:04:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProfileProviderSecondary](
    [ProfileId] [int] NOT NULL,
    [ProviderId] [int] NOT NULL,
 CONSTRAINT [PK_CyberSecurityProfileCredentialProviderSecondary] PRIMARY KEY CLUSTERED 
(
    [ProfileId] ASC,
    [ProviderId] 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
/****** Object:  Table [dbo].[Provider]    Script Date: 25/02/2016 3:04:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Provider](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [NameEng] [nvarchar](250) NOT NULL,
    [NameFra] [nvarchar](250) NOT NULL,
    [DescriptionEng] [nvarchar](max) NULL,
    [DescriptionFra] [nvarchar](max) NULL,
    [ParentId] [int] NULL,
 CONSTRAINT [PK_CredentialProviders] PRIMARY KEY CLUSTERED 
(
    [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] TEXTIMAGE_ON [PRIMARY]

GO
ALTER TABLE [dbo].[Profile]  WITH CHECK ADD  CONSTRAINT [FK_AuthorizedToOperate] FOREIGN KEY([AuthorizedToOperateTypeId])
REFERENCES [dbo].[AuthorizedType] ([Id])
GO
ALTER TABLE [dbo].[Profile] CHECK CONSTRAINT [FK_AuthorizedToOperate]
GO
ALTER TABLE [dbo].[Profile]  WITH CHECK ADD  CONSTRAINT [FK_ProvidersPrimary] FOREIGN KEY([ProviderPrimaryId])
REFERENCES [dbo].[Provider] ([Id])
GO
ALTER TABLE [dbo].[Profile] CHECK CONSTRAINT [FK_ProvidersPrimary]
GO
ALTER TABLE [dbo].[Profile]  WITH CHECK ADD  CONSTRAINT [FK_ProvidersSecondary] FOREIGN KEY([ProviderSecondaryId])
REFERENCES [dbo].[Provider] ([Id])
GO
ALTER TABLE [dbo].[Profile] CHECK CONSTRAINT [FK_ProvidersSecondary]
GO
ALTER TABLE [dbo].[Profile]  WITH CHECK ADD  CONSTRAINT [FK_Profile_IntegrationToolkit] FOREIGN KEY([IntegrationToolkitId])
REFERENCES [dbo].[IntegrationToolkit] ([Id])
GO
ALTER TABLE [dbo].[Profile] CHECK CONSTRAINT [FK_Profile_IntegrationToolkit]
GO
ALTER TABLE [dbo].[ProfileIntegrationToolKit]  WITH CHECK ADD  CONSTRAINT [FK_ProfileIntegrationToolKit_Profile] FOREIGN KEY([ProfileId])
REFERENCES [dbo].[Profile] ([Id])
GO
ALTER TABLE [dbo].[ProfileIntegrationToolKit] CHECK CONSTRAINT [FK_ProfileIntegrationToolKit_Profile]
GO
ALTER TABLE [dbo].[ProfileIntegrationToolKit]  WITH CHECK ADD  CONSTRAINT [FK_ProfileIntegrationToolKit_IntegrationToolkit] FOREIGN KEY([ProfileId])
REFERENCES [dbo].[IntegrationToolkit] ([Id])
GO
ALTER TABLE [dbo].[ProfileIntegrationToolKit] CHECK CONSTRAINT [FK_ProfileIntegrationToolKit_IntegrationToolkit]
GO
ALTER TABLE [dbo].[ProfileProviderPrimary]  WITH CHECK ADD  CONSTRAINT [FK_ProfileProviderPrimary_Provider] FOREIGN KEY([ProfileId])
REFERENCES [dbo].[Provider] ([Id])
GO
ALTER TABLE [dbo].[ProfileProviderPrimary] CHECK CONSTRAINT [FK_ProfileProviderPrimary_Provider]
GO
ALTER TABLE [dbo].[ProfileProviderPrimary]  WITH CHECK ADD  CONSTRAINT [FK_ProfileProviderPrimary_Profile] FOREIGN KEY([ProfileId])
REFERENCES [dbo].[Profile] ([Id])
GO
ALTER TABLE [dbo].[ProfileProviderPrimary] CHECK CONSTRAINT [FK_ProfileProviderPrimary_Profile]
GO
ALTER TABLE [dbo].[ProfileProviderSecondary]  WITH CHECK ADD  CONSTRAINT [FK_ProfileProviderSecondary_Provider] FOREIGN KEY([ProviderId])
REFERENCES [dbo].[Provider] ([Id])
GO
ALTER TABLE [dbo].[ProfileProviderSecondary] CHECK CONSTRAINT [FK_ProfileProviderSecondary_Provider]
GO
ALTER TABLE [dbo].[ProfileProviderSecondary]  WITH CHECK ADD  CONSTRAINT [FK_ProfileProviderSecondary_Profile] FOREIGN KEY([ProfileId])
REFERENCES [dbo].[Profile] ([Id])
GO
ALTER TABLE [dbo].[ProfileProviderSecondary] CHECK CONSTRAINT [FK_ProfileProviderSecondary_Profile]
GO

Am I doing something wrong? 难道我做错了什么? I've tried re-creating the constraints in SSMS but same result. 我尝试过在SSMS中重新创建约束,但结果相同。


Edit 1- If i try reducing the amount of tables I import. 编辑1-如果我尝试减少导入的表的数量。 I get similar issues. 我遇到类似的问题。

实体框架的一小部分

That's a sneaky one! 那是一个偷偷摸摸的! See this part of the DDL: 请参阅DDL的这一部分:

ALTER TABLE [dbo].[ProfileProviderPrimary]  WITH CHECK
ADD  CONSTRAINT [FK_ProfileProviderPrimary_Provider]

    FOREIGN KEY([ProfileId])

REFERENCES [dbo].[Provider] ([Id])

I indented the errant line. 我缩进了错误的行。 The foreign key to Provider should be off ProviderId , just as in ProfileProviderSecondary . Provider的外键应该关闭ProviderId ,就像ProfileProviderSecondary The same error occurs in ProfileIntegrationToolKit . ProfileIntegrationToolKit发生相同的错误。

Correct this, remove the junctions from the edmx, update the model from the database and you're ready to go. 对此进行更正,从edmx中删除结点,从数据库中更新模型,就可以开始了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM