SQL Server multiple cascade path issue - is my model faulty?

主宰稳场 提交于 2019-12-11 02:42:53

问题


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:

  1. Deletion of a Tournament must cascade to Event, Contestant and EventContestant
  2. Deletion of an Event must cascade to EventContestant
  3. Deletion of a Contestant must cascade to EventContestant
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!