title: Entity Framework Core: How to solve Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths
body: <p>I'm using Entity Framework Core with Code First approach but recieve following error when updating the database:</p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint 'FK_AnEventUsers_Users_UserId' on table 'AnEventUsers' 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. </p>
  </blockquote>
  
  <p>My entities are these: </p>
  
  <pre><code>public class AnEvent
  {
      public int AnEventId { get; set; }
      public DateTime Time { get; set; }
      public Gender Gender { get; set; }
      public int Duration { get; set; }
      public Category Category { get; set; }
      public int MinParticipants { get; set; }
      public int MaxParticipants { get; set; }
      public string Description { get; set; }
      public Status EventStatus { get; set; }
      public int MinAge { get; set; }
      public int MaxAge { get; set; }
      public double Longitude { get; set; }
      public double Latitude { get; set; }
  
      public ICollection&lt;AnEventUser&gt; AnEventUsers { get; set; }
  
      public int UserId { get; set; }
      public User User { get; set; }
  }
  
  
  public class User
  {
      public int UserId { get; set; }
      public int Age { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public Gender Gender { get; set; }
      public double Rating { get; set; }
  
      public ICollection&lt;AnEventUser&gt; AnEventUsers { get; set; }
  }
  
  public class AnEventUser
  {
      public int AnEventId { get; set; }
      public AnEvent AnEvent { get; set; }
  
      public int UserId { get; set; }
      public User User { get; set; }
  
  }
  
  public class ApplicationDbContext:DbContext
  {
      public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options):base(options)
      { }
  
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;AnEventUser&gt;()
              .HasOne(u =&gt; u.User).WithMany(u =&gt; u.AnEventUsers).IsRequired().OnDelete(DeleteBehavior.Restrict);
  
  
          modelBuilder.Entity&lt;AnEventUser&gt;()
              .HasKey(t =&gt; new { t.AnEventId, t.UserId });
  
          modelBuilder.Entity&lt;AnEventUser&gt;()
              .HasOne(pt =&gt; pt.AnEvent)
              .WithMany(p =&gt; p.AnEventUsers)
              .HasForeignKey(pt =&gt; pt.AnEventId);
  
          modelBuilder.Entity&lt;AnEventUser&gt;()
              .HasOne(eu =&gt; eu.User)
              .WithMany(e =&gt; e.AnEventUsers)
              .HasForeignKey(eu =&gt; eu.UserId);
  
      }
  
      public DbSet&lt;AnEvent&gt; Events { get; set; }
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;AnEventUser&gt; AnEventUsers { get; set; }
  }
  </code></pre>
  
  <p>The issue I thought was that if we delete a User the reference to the AnEvent will be deleted and also the reference to AnEventUser will also be deleted, since there is a reference to AnEventUser from AnEvent as well we get cascading paths. But I remove the delete cascade from User to AnEventUser with:</p>
  
  <pre><code> modelBuilder.Entity&lt;AnEventUser&gt;()
          .HasOne(u =&gt; u.User).WithMany(u =&gt; u.AnEventUsers).IsRequired().OnDelete(DeleteBehavior.Restrict);
  </code></pre>
  
  <p>But the error doesn't get resolved, does anyone see what is wrong? Thanks!</p>