0

I'm trying to set the Model builder cascade property but I'm not getting the syntax correct.

Simple database:

I have a class (ReqForBetaReader):

public class ReqForBetaReader
{
   public ReqForBetaReader()
   {
      Tags = new List<TagForReqBeta>();
   }

        public Guid Id { get; set; }
        public string Title { get; set; }
        public string Synopsis { get; set; }
        public int WordCount { get; set; }
        public Guid AuthorId { get; set; }
        public virtual User Author { get; set; }
        public DateTime UpdatedOn { get; set; }
        public virtual ICollection<TagForReqBeta> Tags { get; set; }
}

I have my TagForReqBeta class

 public class TagForReqBeta
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public Guid ReqForBetaReaderId { get; set; }
        public virtual ReqForBetaReader ReqForBetaReader { get; set; }

        [Required]
        public Guid UserId { get; set; }
        public virtual User User { get; set; }
        public DateTime AddedOn { get; set; }
    }

So a ReqForBeta can have multiple tags.

When I ran Update-Database, I got the dreaded:

Introducing FOREIGN KEY constraint 'FK_TagsOnRequestForBetaReaders_RequestsForBetaReaders_ReqForBetaReaderId' on table 'TagsOnRequestForBetaReaders' 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.

I believe this means that when I delete a ReqForBeta, I should also delete all the TagForReqBeta as well, but I'm having trouble with the syntax. Any help?

protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<ReqForBetaReader>()
                .HasMany<TagForReqBeta>(x => x.Tags)
                .WithOne<ReqForBetaReader>(x => x.ReqForBetaReaderId)
                .OnDelete(DeleteBehavior.Cascade);

            base.OnModelCreating(builder);
        }
J.G.Sable
  • 1,258
  • 4
  • 26
  • 62
  • Does this answer your question? [Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?](https://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths) – David Browne - Microsoft May 24 '21 at 16:56
  • 1
    Both these entities have relationship with `User` - `ReqForBetaReader.Author` and `TagForReqBeta.User`, which creates the "multiple cascade path" from `User` to `TagForReqBeta`. You have to turn off cascade delete for at least one of these relationships. – Ivan Stoev May 24 '21 at 17:12

1 Answers1

0

Since ReqForBetaReaderId is not nullable, when you delete ReqForBetaReader record you will have to delete TagForReqBeta.

Since as I understand the same tag can be used for several records you have to make ReqForBetaReaderId nullable

[Required]
public Guid? ReqForBetaReaderId { get; set; }

builder.Entity<ReqForBetaReader>()
                .HasMany<TagForReqBeta>(x => x.Tags)
                .WithOne<ReqForBetaReader>(x => x.ReqForBetaReaderId)
                .OnDelete(DeleteBehavior.ClientSetNull);

and as @IvanStoev noticed you have the same problem with User

Serge
  • 40,935
  • 4
  • 18
  • 45