SQL Server 2016 无法将系统版本控制添加到关系 table

SQL Server 2016 Cannot add system versioning to relationship table

SQL Server 2016 系统版本控制很酷。我正在使用免费的开发者版本。谢谢 MS!

我无法弄清楚它是否会为我提供多对多关系的版本控制。我有一个包含角色集合的用户对象,反之亦然。 Entity Framework 生成了 UserRoles table,它持有 UserRoles 之间的关系。使用这篇文章 http://sqlhints.com/tag/modify-existing-table-as-system-versioned-temporal-table/.

,我能够为 UserRoles table 启用系统版本控制

但是,我 UserRoles 无法开机。我收到一个错误

Setting SYSTEM_VERSIONING to ON failed because table has a FOREIGN KEY with cascading DELETE or UPDATE.

这是否意味着我们无法知道多对多关系的版本控制?

例如。

所以,如果我想知道 6 月 1 日的用户状态,我认为只有在 UserRoles 上打开系统版本控制才有可能,但这是行不通的。

SQL Server 2016 是否支持此操作?如果不行,有没有其他方法可以实现?

听起来像是 ON UPDATE CASCADE 或 ON UPDATE DELETE 外键的问题。删除级联并将其替换为知道并处理正确关系的删除过程,你应该没问题。

就个人而言,我喜欢知道我的 deletes/updates 在做什么,而不是相信关系来处理所有这些事情。我可以看到潜在的锁定问题,并且知道有时我真的想阻止更新或删除,而不是让它级联通过所有看不见的表。

当前 table 不允许使用 ON DELETE CASCADE 和 ON UPDATE CASCADE。换句话说,当temporal table在外键关系中引用table时(对应sys.foreign_keys中的parent_object_id)不允许使用CASCADE选项。要解决此限制,请使用应用程序逻辑或后触发器来保持主键 table 中删除的一致性(对应于 sys.foreign_keys 中的 referenced_object_id)。如果主键 table 是临时的并且引用 table 是非临时的,则没有这样的限制。

请务必注意,在临时表中对 FOREIGN KEY 约束使用 CASCADE 的限制仅适用于 SQL Server 2016。在 SQL Server 2017 中,此限制不再存在。

这是official documentation中的相关部分:

ON DELETE CASCADE and ON UPDATE CASCADE are not permitted on the current table. In other words, when temporal table is referencing table in the foreign key relationship (corresponding to parent_object_id in sys.foreign_keys) CASCADE options are not allowed. To work around this limitation, use application logic or after triggers to maintain consistency on delete in primary key table (corresponding to referenced_object_id in sys.foreign_keys). If primary key table is temporal and referencing table is non-temporal, there's no such limitation.

> NOTE: This limitation applies to SQL Server 2016 only. CASCADE options are supported in SQL Database and SQL Server 2017 starting from CTP 2.0.