简体   繁体   English

实体框架-模型自引用依赖项不级联删除

[英]Entity Framework - Model Self-Referencing Dependencies Not Cascading Delete

I have a Group model/table: 我有一个组模型/表:

namespace Project
{
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    #endregion

    [Table("Groups")]
    public class Group
    {

    public Group()
    {
        this.Pk = Guid.NewGuid();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Key]
    [Required]
    public Guid Pk
    {
        get;
        set;
    }

    public virtual ICollection<Group> Parents
    {
        get;
        set;
    }

    public virtual ICollection<Group> Children
    {
        get;
        set;
    }

    public virtual ICollection<Document> Documents
    {
        get;
        set;
    }

    ...
}

A group can have many groups as either a child or parent, as well as many documents. 一个组可以有多个组,既可以是子组也可以是父组,也可以有许多文档。

I have the relationships wired up in Fluent API as so: 我在Fluent API中建立了关联关系,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<Group>()
                    .HasMany(e => e.Parents)
                    .WithMany(e => e.Children)
                    .Map(e =>
                         {
                             e.MapLeftKey("ParentPk");
                             e.MapRightKey("ChildPk");
                             e.ToTable("GroupMappings");
                         });

        modelBuilder.Entity<Group>()
                    .HasMany(e => e.Documents)
                    .WithMany(e => e.Groups)
                    .Map(e =>
                         {
                             e.MapLeftKey("DocumentPk");
                             e.MapRightKey("GroupPk");
                             e.ToTable("DocumentMappings");                           
                         });
 }

When I generate the SQL for these models, the relationship between Groups and Documents (DocumentMappings) has ON DELETE CASCASDE: 当我为这些模型生成SQL时,组和文档(DocumentMappings)之间的关系具有ON DELETE CASCASDE:

ALTER TABLE [dbo].[DocumentMappings] ADD CONSTRAINT [FK_dbo.DocumentMappings_dbo.Documents_GroupPk] FOREIGN KEY ([GroupPk]) REFERENCES [dbo].[Documents] ([Pk]) ON DELETE CASCADE

But the GroupMappings does not: 但是,GroupMappings不会:

ALTER TABLE [dbo].[GroupMappings] ADD CONSTRAINT [FK_dbo.GroupMappings_dbo.Groups_ParentPk] FOREIGN KEY ([ParentPk]) REFERENCES [dbo].[Groups] ([Pk])
ALTER TABLE [dbo].[GroupMappings] ADD CONSTRAINT [FK_dbo.GroupMappings_dbo.Groups_ChildPk] FOREIGN KEY ([ChildPk]) REFERENCES [dbo].[Groups] ([Pk])

Visual representation of the tables: 表格的外观: 表关系

What could be wrong with my mappings for Group (Parent/Children) that is causing ON DELETE CASCADE to not be "enabled"? 我的组(父/子)映射可能导致“未启用启用DELETE CASCADE”的问题是什么? Is it related to it being a reference on the same type? 它是否与相同类型的引用有关?

Disclaimer: I have seen a few posts saying this can not be done because you "can't use CASCADE DELETE on self referencing table in SQL SERVER." 免责声明:我看到一些帖子说无法完成,因为您“无法在SQL SERVER中的自引用表上使用CASCADE DELETE”。 ( Entity Framework 6 Code-First cascade delete on self referencing entity ). 自引用实体上的Entity Framework 6 Code-First级联删除 )。 I am using a mapping table in this case, so this should not be the situation. 在这种情况下,我使用的是映射表,因此不应该是这种情况。

Is it related to it being a reference on the same type? 它是否与相同类型的引用有关?

Yes it is. 是的。 The GroupMappings table has two foreign keys. GroupMappings表具有两个外键。 If you try to set them bot to cascading delete you will get this exception: 如果尝试将它们设置为级联删除,则会出现此异常:

Introducing FOREIGN KEY constraint 'contraintName' on table 'GroupMappings' may cause cycles or multiple cascade paths. 在表“ GroupMappings”上引入FOREIGN KEY约束“ contraintName”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

With two foreign keys, if you delete a Group there would multiple cascade paths to deleting GroupMappings . 使用两个外键,如果删除一个Group ,将有多个级联路径来删除GroupMappings Sql Server has this restriction that it doesn't allow this, even when the paths end up at the same table. Sql Server具有此限制,即使路径最终位于同一张表,它也不允许这样做。

EF has knowledge of this restriction and doesn't want to choose for you which FK it equips with cascaded delete. EF了解此限制,并且不想为您选择它配备级联删除的FK。 It can't do both. 它不能同时做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM