zoukankan      html  css  js  c++  java
  • SQL Server 与 Entity Framework 级联删除

    更新: 

    ef core issue https://github.com/aspnet/EntityFrameworkCore/issues/4073

    这种情况可以 cascade delete and update

    这两种都算 parent child 不可以 cascade delete and update,

    这种比较特别,如果只是 1 个 foreight key 那么是可以 cascade delete and update 的

    但是如果 key 是 composition 那么 cascade delete 可以, cascade update 则不可以哦

    SQL Server 级联设置我就不多说了,网上很多教程.

    我想提的是 cycles or multiple cascade paths 的问题.

    简单的说如果你的级联设置不是一个树型,而是一个带有循环的图,那么这个error就会出现了。

    refer http://stackoverflow.com/questions/851625/foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths
    refer https://www.zhihu.com/question/25379813 (级联因该是树形结构的)

    所以在使用Entity Code First 时,建议大家把级联关闭

    builder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
    builder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

    自己去SQL里头设置,而遇到 cycles 的话,最好的处理方式应该是使用触发器,拦截delete,先删除级联在删除主key 

    当然还有一个方式是通过 EF 加载所有级联出来,然后删除. 这样可以不需要在sql做任何设置,只是性能有点伤, 不推荐使用. 

  • 相关阅读:
    hdoj:2033
    hdoj:2032
    hdoj:2031
    hdoj:2029
    hdoj:2028
    hdoj:2027
    hdoj:2024
    hdoj:2023
    hdoj:2022
    hdoj:题目分类
  • 原文地址:https://www.cnblogs.com/keatkeat/p/5397055.html
Copyright © 2011-2022 走看看