简体   繁体   中英

Introducing FOREIGN KEY constraint with ON DELETE NO ACTION doesn't working

I have 4 tables with between them foreign keys as: Classification - Classification level - Classification value - Classification language

on add-migration it is ok, but when running update-database I get the error:

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.

The tables as mentioned in the error are defined as:

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; } 
    }

and

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; }
    }

I have added the delete behavior line in my DBContect class as:

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

Also, I have put this on the foreign keys between all tables of the cascading structure.

            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);

Further, I have tried to set it for all foreign keys with:

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

As I don't need the cascading delete behavior.

After these different tries I did again an add-migration and update-database. But the error is still the same.

Any suggestions?

Finally, I went into the migration files and changed everywhere from cascade to restrict. As I don't want to use cascade anyway. And now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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