title: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?
body: <p>I've been wrestling with this for a while and can't quite figure out what's happening.  I have a Card entity which contains Sides (usually 2) - and both Cards and Sides have a Stage.  I'm using EF Codefirst migrations and the migrations are failing with this error: </p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint 'FK_dbo.Sides_dbo.Cards_CardId' on
    table 'Sides' may cause cycles or multiple cascade paths. Specify ON
    DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY
    constraints.</p>
  </blockquote>
  
  <p>Here's my <strong>Card</strong> entity: </p>
  
  <pre><code>public class Card
  {
      public Card()
      {
          Sides = new Collection&lt;Side&gt;();
          Stage = Stage.ONE;
      }
  
      [Key]
      [Required]
      public virtual int CardId { get; set; }
  
      [Required]
      public virtual Stage Stage { get; set; }
  
      [Required]
      [ForeignKey("CardId")]
      public virtual ICollection&lt;Side&gt; Sides { get; set; }
  }
  </code></pre>
  
  <p>Here's my <strong>Side</strong> entity: </p>
  
  <pre><code>public class Side
  {
      public Side()
      {
          Stage = Stage.ONE;
      }
  
      [Key]
      [Required]     
      public virtual int SideId { get; set; } 
  
      [Required]
      public virtual Stage Stage { get; set; }
  
      [Required]
      public int CardId { get; set; }
  
      [ForeignKey("CardId")]
      public virtual Card Card { get; set; }
  
  }
  </code></pre>
  
  <p>And here's my <strong>Stage</strong> entity: </p>
  
  <pre><code>public class Stage
  {
      // Zero
      public static readonly Stage ONE = new Stage(new TimeSpan(0, 0, 0), "ONE");
      // Ten seconds
      public static readonly Stage TWO = new Stage(new TimeSpan(0, 0, 10), "TWO");
  
      public static IEnumerable&lt;Stage&gt; Values
      {
          get
          {
              yield return ONE;
              yield return TWO;
          }
  
      }
  
      public int StageId { get; set; }
      private readonly TimeSpan span;
      public string Title { get; set; }
  
      Stage(TimeSpan span, string title)
      {
          this.span = span;
          this.Title = title;
      }
  
      public TimeSpan Span { get { return span; } }
  }
  </code></pre>
  
  <p>What's odd is that if I add the following to my Stage class: </p>
  
  <pre><code>    public int? SideId { get; set; }
      [ForeignKey("SideId")]
      public virtual Side Side { get; set; }
  </code></pre>
  
  <p>The migration runs successfully.  If I open up SSMS and look at the tables, I can see that <code>Stage_StageId</code> has been added to <code>Cards</code> (as expected/desired), however <code>Sides</code> contains no reference to <code>Stage</code> (not expected).  </p>
  
  <p>If I then add </p>
  
  <pre><code>    [Required]
      [ForeignKey("StageId")]
      public virtual Stage Stage { get; set; }
      public int StageId { get; set; }
  </code></pre>
  
  <p>To my Side class, I see <code>StageId</code> column added to my <code>Side</code> table.  </p>
  
  <p>This is working, but now throughout my application, any reference to <code>Stage</code> contains a <code>SideId</code>, which is in some cases totally irrelevant.  <strong>I'd like to just give my <code>Card</code> and <code>Side</code> entities a <code>Stage</code> property based on the above Stage class without polluting the stage class with reference properties if possible</strong>... what am I doing wrong?  </p>