关联映射细节混乱?

时间:2010-06-04 15:14:50

标签: entity-framework

我从未理解为什么EntityFramework中的关联看起来像他们在“映射详细信息”窗口中所做的那样。

当我为关联选择2个表之间的行时,例如FK_ApplicationSectionsNodes_FormItems,它显示了这个:

Association
  Maps to ApplicationSectionNodes
    FormItems
      (key symbol) FormItemId:Int32     <-->     FormItemId:int
    ApplicationSectionNodes
      (key symbol) NodeId:Int32         <-->     (key symbol) NodeId : int

幸运的是,这个是基于我的数据库中的外键约束自动创建的,但是每当没有约束时,我就很难手动创建关联(当数据库没有图表设置时)因为我不喜欢不了解关联的映射细节。

FormItems表具有主键标识列FormItemId,而ApplicationSectionNodes包含FormItemId列,该列是外键并具有NodeId作为主键标识列。

对我来说真正没有意义的是,当NodeId与外键关系没有任何关系时,为什么协会有关于NodeId的任何内容?(这对自己来说更加困惑引用关系,但也许如果我能理解上述情况,我会有更好的处理方法。)

CREATE TABLE [dbo].[ApplicationSectionNodes](
    [NodeID] [int] IDENTITY(1,1) NOT NULL,
    [OutlineText] [varchar](5000) NULL,
    [ParentNodeID] [int] NULL,
    [FormItemId] [int] NULL,
 CONSTRAINT [PK_ApplicationSectionNodes] PRIMARY KEY CLUSTERED 
(
    [NodeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [UQ_ApplicationSectionNodesFormItemId] UNIQUE NONCLUSTERED 
(
    [FormItemId] 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

ALTER TABLE [dbo].[ApplicationSectionNodes]  WITH NOCHECK ADD  CONSTRAINT [FK_ApplicationSectionNodes_ApplicationSectionNodes] FOREIGN KEY([ParentNodeID])
REFERENCES [dbo].[ApplicationSectionNodes] ([NodeID])
GO
ALTER TABLE [dbo].[ApplicationSectionNodes] NOCHECK CONSTRAINT [FK_ApplicationSectionNodes_ApplicationSectionNodes]
GO
ALTER TABLE [dbo].[ApplicationSectionNodes]  WITH NOCHECK ADD  CONSTRAINT [FK_ApplicationSectionNodes_FormItems] FOREIGN KEY([FormItemId])
REFERENCES [dbo].[FormItems] ([FormItemId])
GO
ALTER TABLE [dbo].[ApplicationSectionNodes] NOCHECK CONSTRAINT [FK_ApplicationSectionNodes_FormItems]
GO

FormItems表:

CREATE TABLE [dbo].[FormItems](
    [FormItemId] [int] IDENTITY(1,1) NOT NULL,
    [FormItemType] [int] NULL,
 CONSTRAINT [PK_FormItems] PRIMARY KEY CLUSTERED 
(
    [FormItemId] 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
ALTER TABLE [dbo].[FormItems]  WITH NOCHECK ADD  CONSTRAINT [FK_FormItems_FormItemTypes] FOREIGN KEY([FormItemType])
REFERENCES [dbo].[FormItemTypes] ([FormItemTypeId])
GO
ALTER TABLE [dbo].[FormItems] NOCHECK CONSTRAINT [FK_FormItems_FormItemTypes]
GO

1 个答案:

答案 0 :(得分:0)

这看起来不正确,您是否可以将数据库中的实际外键定义添加为SSMS的外键编辑器对话框中的图像,或者来自SSMS的数据库脚本中的这些表。

如果未在外键中列出,EF不会随机拉入NodeId。

检查您的数据库。