引入带有ON DELETE NO ACTION的FOREIGN KEY约束不起作用

时间:2019-08-27 10:40:51

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

我有4个表,它们之间的外键为: 分类-分类级别-分类值-分类语言

在添加迁移上可以,但是在运行更新数据库时出现错误:

Introducing FOREIGN KEY constraint 'FK_dbClassificationValue_dbClassificationLevel_ClassificationLevelId' on table 'dbClassificationValue' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.

错误中提到的表定义为:

public class SuClassificationLevelModel
    {
        public int Id { get; set; }
        public int ClassificationId { get; set; }
        public int Sequence { get; set; }
        public bool DateLevel { get; set; }
        public bool OnTheFly { get; set; }
        public bool Alphabetically { get; set; }
        public bool CanLink { get; set; }
        public bool InDropDown { get; set; }
        public Guid CreatorId { get; set; }
        public Guid ModifierId { get; set; }
        public DateTime ModifiedDate { get; set; }
        public DateTime CreatedDate { get; set; }

        public virtual SuClassificationModel Classification { get; set; }
        public virtual ICollection<SuClassificationLevelLanguageModel> ClassificationLevelLanguages { get; set; }
        public virtual ICollection<SuClassificationValueModel> ClassificationValues { get; set; } 
    }

public class SuClassificationValueModel
    {
        public int Id { get; set; }
        public int ClassificationLevelId { get; set; }
        public int ParentValueId { get; set; }
        public DateTimeOffset DateFrom { get; set; }
        public DateTimeOffset DateTo { get; set; }
        public virtual SuClassificationLevelModel ClassificationLevel { get; set; }
        public virtual ICollection<SuClassificationValueLanguageModel> ClassificationValueLanguages { get; set; }
    }

我已经在DBContect类中添加了delete行为行:

    modelBuilder.Entity<SuClassificationValueModel>()
        .HasOne(u => u.ClassificationLevel)
        .WithMany(u => u.ClassificationValues)
        .HasForeignKey(u => u.ClassificationLevelId)
        .OnDelete(DeleteBehavior.Restrict);

此外,我将其放在级联结构的所有表之间的外键上。

            modelBuilder.Entity<SuClassificationLevelModel>()
                .HasOne(u => u.Classification)
                .WithMany(u => u.ClassificationLevels)
                .HasForeignKey(u => u.ClassificationId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<SuClassificationValueModel>()
                .HasOne(u => u.ClassificationLevel)
                .WithMany(u => u.ClassificationValues)
                .HasForeignKey(u => u.ClassificationLevelId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<SuClassificationValueLanguageModel>()
                .HasOne(u => u.ClassificationValue)
                .WithMany(u => u.ClassificationValueLanguages)
                .HasForeignKey(u => u.ClassificationValueId)
                .OnDelete(DeleteBehavior.Restrict);

此外,我尝试使用以下方法为所有外键设置它:

foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
            {
                relationship.DeleteBehavior = DeleteBehavior.Restrict;
            }

因为我不需要级联的删除行为。

在进行了这些不同的尝试之后,我再次执行了add-migration和update-database。但是错误仍然相同。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

最后,我进入了迁移文件,并从级联更改为限制。因为我还是不想使用级联。现在就可以了。