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

时间:2016-02-25 20:16:23

标签: c# database entity-framework database-design

我们正在尝试使用数据库第一实体框架设置新的数据库和应用程序。

除了这一堆表之外,一切都运行良好。 实体框架未设置关联,而是在3个实例中的2个中显示联结表。

在图片中,ProfileIntegrationToolkit和ProfileProviderPrimary应该消失,而应该是关联。

Entity framework diagram

这是重现表格的代码

/****** 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

我做错了吗? 我尝试在SSMS中重新创建约束但结果相同。

编辑1- 如果我尝试减少我导入的表的数量。我遇到了类似的问题。

Small subset from Entity framework

1 个答案:

答案 0 :(得分:1)

那是一个偷偷摸摸的人!请参阅DDL的这一部分:

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

    FOREIGN KEY([ProfileId])

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

我缩进了错误的行。 Provider的外键应该关闭ProviderId,就像在ProfileProviderSecondary中一样。 ProfileIntegrationToolKit中出现同样的错误。

更正此问题,从edmx中删除联结,从数据库更新模型,然后就可以开始了。