简体   繁体   English

实体框架中的级联删除

[英]Cascade delete in entity framework

I am having problems setting up relationships in entity framework 6 and making cascade deletion work. 我在实体框架6中建立关系并进行级联删除时遇到问题。

I have the following objects 我有以下对象

public class Module {
    public long Id { get; set; };
    public virtual ICollection<Group> Groups { get; set; };
}

public class Group {
    public long Id { get; set; };
    public virtual ICollection<Field> Fields { get; set; };
}

public class Field {
    public long Id { get; set; };
    public virtual FieldLink Link { get; set; };
}

public class FieldLink {
    public long Id { get; set; };
    public virtual Module LinkToModule { get; set; };
}

Now a module has groups, a group has fields, a field MAY have a link. 现在,一个模块具有组,一个组具有字段,一个字段可以具有链接。 A link will have a LinkToModule, but this can be a different module then the one that the parent field/group belongs too. 链接将具有LinkToModule,但这可以是与父字段/组所属的模块不同的模块。

I have setup my relationships like so 我已经建立了这样的关系

public ModuleConfig() 
{
    this.ToTable("Module");
}

public FieldGroupConfig() 
{
    this.ToTable("FieldGroup");

    // relationships
    this.HasRequired(e => e.Module)
        .WithMany(e => e.Groups)
        .HasForeignKey(e => e.ModuleId);
}  

public FieldConfig() 
{
    this.ToTable("Field");

    this.HasRequired(e => e.FieldGroup)
        .WithMany(e => e.Fields)
        .HasForeignKey(e => e.FieldGroupId);


    this.HasOptional(e => e.Link)
        .WithRequired(e => e.Field);

}

public FieldLinkConfig() 
{
    this.ToTable("FieldLink");

    this.HasRequired(e => e.LinkToModule)
        .WithMany()
        .HasForeignKey(e => e.LinkToModuleId);
}

Now i am running my tests, and i get the following error 现在我正在运行测试,并且出现以下错误

Test method ModuleServiceTests.ModuleService_DeleteAsync_ByEntity threw exception: 

System.Data.SqlClient.SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.FieldLink_dbo.Field_Id". The conflict occurred in database "TestDb", table "dbo.FieldLink", column 'Id'.

For if i check the relationship, it is between table Field.Id > FieldLink.Id and the DELETE rule is set as NO ACTION. 对于如果我检查的关系,它是在表Field.Id> FieldLink.Id之间,并且DELETE规则设置为NO ACTION。 Fine, so i guess i need to update that relationship and use WillCascadeOnDelete(true) 很好,所以我想我需要更新这种关系并使用WillCascadeOnDelete(true)

So i updated the code in FieldConfig from 所以我从更新了FieldConfig中的代码

this.HasOptional(e => e.Link)
    .WithRequired(e => e.Field);

to

this.HasOptional(e => e.Link)
    .WithRequired(e => e.Field)
    .WillCascadeOnDelete(true);

But now when i try to run my test, the database isnt even created and i get the error saying 但是现在当我尝试运行测试时,甚至没有创建数据库,并且出现错误提示

Initialization method Test.TestInitialize threw exception. 
System.Data.SqlClient.SqlException: Introducing FOREIGN KEY constraint 'FK_dbo.FieldLink_dbo.Field_Id' on table 'FieldLink' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Can someone please help? 有人可以帮忙吗? Have i setup a relationship incorrectly, am i going the wrong way about this? 我是否建立了不正确的关系,这是我走错了路吗?

MS SQL Server does not supports cycles in the cascade deletes actions. MS SQL Server不支持级联删除操作中的循环。 You need to choose just one of the two directions to cascade deletes or find a workaround like in this answer (example of the trigger is here in Listing 6 ). 你需要选择的只是两个方向的一个级联删除或在此找到这样一个解决方法的答案 (例如,触发的是这里清单6 )。 This answer also contains some insights. 这个答案也包含一些见解。

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

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