如何删除多对多关系(仅联接表)

时间:2019-08-14 03:28:52

标签: asp.net-core-mvc entity-framework-core

我在ASP.NET Core应用程序中使用Entity Framework Core。我希望能够删除多对多关系表(其中只有table1Id和table2Id列链接多对多关系)中的联接链接行,但是我唯一的选择是

1)SetNull(我假设尝试将TABLE2上的table2Id列设置为null,因为我无法执行更新数据库,它告诉我原因是因为它无法设置外键id列为空)

2)限制(这是一项创可贴修复,因为它允许我进行推送,但是现在我的用户无法删除,直到他们在删除之前撤消了所有关系)

3)级联(试图删除所有内容,这绝对不是我想要的)

似乎不仅仅是“ DeleteRelationship”选项

我试图将链接类中的id属性设置为可为空(请参见代码)

public class PregenProfessionArchetypeModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<PregenProfessionArchetype_ParentSkill_LinkModel> TrainedParentSkills { get; set; } = new List<PregenProfessionArchetype_ParentSkill_LinkModel>();
    public ICollection<PregenProfessionArchetype_ChildSkill_LinkModel> ChildSkills { get; set; } = new List<PregenProfessionArchetype_ChildSkill_LinkModel>();
    public ICollection<PregenProfessionArchetype_Technique_LinkModel> Techniques { get; set; } = new List<PregenProfessionArchetype_Technique_LinkModel>();
    public ICollection<PregenProfessionArchetype_Talent_LinkModel> Talents { get; set; } = new List<PregenProfessionArchetype_Talent_LinkModel>();
}


public class PregenProfessionArchetype_ChildSkill_LinkModel
{
    public int PregenProfessionArchetypeId { get; set; }
    public PregenProfessionArchetypeModel PregenProfessionArchetype { get; set; }
    public int ChildSkillId { get; set; }
    public ChildSkillModel ChildSkill { get; set; }
}

public class PregenProfessionArchetype_ParentSkill_LinkModel
{
    public int? PregenProfessionArchetypeId { get; set; }
    public PregenProfessionArchetypeModel PregenProfessionArchetype { get; set; }
    public int? ParentSkillId { get; set; }
    public ParentSkillModel ParentSkill { get; set; }
}

public class ChildSkillModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int ParentSkillId { get; set; }
    [Display(Name = "Parent Skill")]
    public ParentSkillModel ParentSkill { get; set; }
    public int Cost { get; set; } = 1;


    public ICollection<PregenProfessionArchetype_ChildSkill_LinkModel> PregenProfessionArchetypeLink { get; set; } = new List<PregenProfessionArchetype_ChildSkill_LinkModel>();
}


        // Application Context File Overwritting (Child is same and left out talent)
        builder.Entity<PregenProfessionArchetype_ParentSkill_LinkModel>()
            .HasKey(ttp => new { ttp.ParentSkillId, ttp.PregenProfessionArchetypeId });

        builder.Entity<PregenProfessionArchetype_ParentSkill_LinkModel>()
            .HasOne<PregenProfessionArchetypeModel>(tm => tm.PregenProfessionArchetype)
            .WithMany(tpp => tpp.TrainedParentSkills)
            .HasForeignKey(tm => tm.PregenProfessionArchetypeId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.Entity<PregenProfessionArchetype_ParentSkill_LinkModel>()
            .HasOne<ParentSkillModel>(tm => tm.ParentSkill)
            .WithMany(tmm => tmm.PregenProfessionArchetypeLink)
            .HasForeignKey(tm => tm.PregenProfessionArchetypeId)
            .OnDelete(DeleteBehavior.Restrict);

我唯一能推送的是将其设置为受限状态。该代码显示了类及其关系的示例

错误消息:SetNull

Failed executing DbCommand (81ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [PregenProfessionArchetype_ParentSkill_LinkModel] ADD CONSTRAINT [FK_PregenProfessionArchetype_ParentSkill_LinkModel_ParentSkillModel_PregenProfessionArchetypeId] FOREIGN KEY ([PregenProfessionArchetypeId]) REFERENCES [ParentSkillModel] ([Id]) ON DELETE SET NULL;

1 个答案:

答案 0 :(得分:1)

如果在删除父级技能时只想删除联接表和父级表行的链接,则可以使用Cascade delete,这是required relationships的默认行为

Delete behaviors在DeleteBehavior枚举器类型中定义,并且可以传递给OnDelete fluent API,以控制是否删除主体/父实体或与依赖/子实体的关系的切断对依赖/子实体的影响。

在您的情况下,PregenProfessionArchetype_ParentSkill_LinkModelPregenProfessionArchetypeModelParentSkillModel的从属/子实体。因此,当删除父项时,由于级联,它从PregenProfessionArchetype_ParentSkill_LinkModel表中的链接将被自动删除,但不会删除前代项。而且,如果您还想删除仅与该父级关联的Pregen,则必须手动进行。

参考:Entity Framework Core Code-First: Cascade delete on a many-to-many relationship