问题
I have the following model:
I want automatic cascade on delete to work - both because it's convenient, keep my data consistent and because it's well supported in Entity Framework Code First. The problem is that this specific model causes multiple cascade path. This results in SQL Server gives me the following error when I try to generate the database through EF Code First:
Introducing FOREIGN KEY constraint 'FK_dbo.Events_dbo.Tournaments_Tournament_Id' on table 'Event' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I understand that when deleting a Tournament
, it would be a race whether or not the deletion of Contestant
or Event
triggers deletion of EventContestant
. SQL Server doesn't appreciate that.
Now the question is, is there anything wrong with my model? Is there anyway else to model this data or some configuring I can do to fix this? I'd like to adhere to the following requirements:
- Deletion of a
Tournament
must cascade toEvent
,Contestant
andEventContestant
- Deletion of an
Event
must cascade toEventContestant
- Deletion of a
Contestant
must cascade toEventContestant
- A relation between Contestant and Event is optional (hence the 0-* multiplicity)
I could solve the issue by introducing a separate primary key in EventContestant, instead of the combined key of the two navigational properties, but then I would break requirement 1 through 3. Any other suggestions?
回答1:
Now the question is, is there anything wrong with my model?
Not necessarily. Diamond-shaped dependencies are fairly common in database modeling.
The problem is that MS SQL Server doesn't support automatic referential actions on such dependencies, even though they can be a perfectly valid situation from the logical standpoint. In other words, your tool is more limiting than it should be.
You'll have to use triggers to implement cascades.
来源:https://stackoverflow.com/questions/21137009/sql-server-multiple-cascade-path-issue-is-my-model-faulty