实体框架核心:循环或多个级联路径

时间:2018-06-20 19:43:20

标签: c# entity-framework-core

我的项目中的数据库包括带有循环链接的表: enter image description here

这是具有流畅配置的实体类:

public class ReportFolder
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public Guid? ParentFolderId { get; set; }
    public virtual ReportFolder ParentFolder { get; set; }
    public virtual List<ReportFolder> ChildFolders { get; set; }
}

public class ReportFolderConfiguration : IEntityTypeConfiguration<ReportFolder>
{
    public void Configure(EntityTypeBuilder<ReportFolder> builder)
    {
        builder.HasKey(c => c.Id);
        builder.Property(c => c.Id).ValueGeneratedOnAdd();

        builder.Property(c => c.Name).IsRequired().HasMaxLength(100);

        builder.HasOne(c => c.ParentFolder)
            .WithMany(c => c.ChildFolders)
            .HasForeignKey(c => c.ParentFolderId)
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasIndex(c => c.Name);
    }
}

问题:尝试通过迁移更新数据库时遇到错误:

  

System.Data.SqlClient.SqlException(0x80131904):在表'ReportFolders'上引入FOREIGN KEY约束'FK_ReportFolders_ReportFolders_ParentFolderId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

0 个答案:

没有答案