2010-06-04 109 views
1

我從來沒有明白爲什麼的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 

回答

0

這看起來不正確,你能添加在數據庫中的實際外鍵定義無論從SSMS的外鍵編輯器對話框或從數據庫腳本中的圖像來自SSMS的這些表格。

EF不會在節點Id具有無規拉,如果它是不是在你的外鍵列。

檢查數據庫。

+0

我添加顯示外鍵創建表的語句。在其他表關係中,儘管外鍵關鍵字不是外鍵約束的一部分,但似乎總是出現外鍵表的主鍵出現在關聯中的情況。 – AaronLS 2010-06-07 14:16:08