我發現了錯誤:SQL Server:兩個指向一個表的外鍵。錯誤:將外鍵約束可能會引起循環或多個級聯路徑
Msg 1785, Level 16, State 0, Line 238
Introducing FOREIGN KEY constraint 'FK_Studios_Members_HeadId' on table 'Studios' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
下面是我有問題的兩個表的簡化版本:
CREATE TABLE [Members]
(
[MemberId] int NOT NULL IDENTITY
)
CREATE TABLE [Studios]
(
[StudioId] int NOT NULL IDENTITY,
[HeadId] int,
[OwnerId] int,
CONSTRAINT [PK_Studios] PRIMARY KEY ([StudioId]),
CONSTRAINT [FK_Studios_Members_OwnerId]
FOREIGN KEY ([OwnerId]) REFERENCES [Members] ([MemberId])
ON DELETE SET NULL,
CONSTRAINT [FK_Studios_Members_HeadId]
FOREIGN KEY ([HeadId]) REFERENCES [Members] ([MemberId])
ON DELETE SET NULL
)
我發現,如果我切換兩個FK的順序,它將始終在第二個錯誤。我不明白爲什麼這會導致級聯問題,因爲它們都具有「ON DELETE SET NULL」。
這是由EF核心代碼生成的,所以我需要這些關係,並且不能在後端的觸發器中進行破解。
我錯過了什麼?
你看到[這](http://stackoverflow.com/questions/851625/foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths)?我不知道它是否有助於回答這個問題,但它幫助我理解了一般問題:-) – SMM
好吧,許多可能的重複之一:http://stackoverflow.com/q/21390621/861716 –