级联在实体框架中删除 [英] Cascade delete in entity framework

查看:149
本文介绍了级联在实体框架中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下对象

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

public class Group {
public long Id {get;组; };
public virtual ICollection< Field>字段{get;组; };
}

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

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

现在一个模块有组,一个组有字段,一个字段可能有一个链接。一个链接将有一个LinkToModule,但这可以是一个不同的模块,那个父域/组属于。



我已经设置了我的关系, p>

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

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

//关系
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);
}

现在我正在运行我的测试,我收到以下错误

 测试方法ModuleServiceTests.ModuleService_DeleteAsync_ByEntity抛出异常:

System.Data.SqlClient.SqlException:DELETE语句与REFERENCE约束FK_dbo.FieldLink_dbo.Field_Id。冲突发生在数据库TestDb中,表dbo.FieldLink,列Id。

如果我检查关系,它在表Field.Id> FieldLink.Id和DELETE之间规则设置为NO ACTION。好的,所以我想我需要更新这种关系,并使用WillCascadeOnDelete(true)



所以我更新了FieldConfig中的代码从$ /

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

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

但是,当我尝试运行我的测试时,数据库甚至没有创建,我得到错误说

 初始化方法Test.TestInitialize抛出异常。 
System.Data.SqlClient.SqlException:表FieldLink上的FOREIGN KEY约束FK_dbo.FieldLink_dbo.Field_Id引入可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

有人可以帮忙吗?我有错误的关系,我错了吗?

解决方案

MS SQL Server不支持级联删除操作。您需要选择两个方向之一来级联删除或找到一种解决方法,例如在这个 answer (触发器的示例是这里清单6 )。此 answer 还包含一些见解。 / p>

I am having problems setting up relationships in entity framework 6 and making cascade deletion work.

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.

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. Fine, so i guess i need to update that relationship and use WillCascadeOnDelete(true)

So i updated the code in FieldConfig from

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. 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). This answer also contains some insights.

这篇关于级联在实体框架中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆