title: Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths causing Database.SetInitializer to not work?
body: <p>My code-first database was working great.  If I made a change to my database context the database would be updated the next time I started the application.  But then I added some models to the database and got this error when I restarted my application:</p>
  
  <p><code>Introducing FOREIGN KEY constraint 'FK_OrderDetails_Orders_OrderId' on table 'OrderDetails' 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. See previous errors.</code></p>
  
  <p>One of the weird things is that if I start the application up again without changing anything, I then get this error:</p>
  
  <p><code>Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.</code></p>
  
  <p>To get the first error to happen again, I have to delete my .mdf and .ldf files (the database) and replace just the .mdf file with a copy from my revision history.</p>
  
  <p><strong>Why in the world is this happening?</strong></p>
  
  <hr>
  
  <p><em>For reference:</em></p>
  
  <p>My Global.asax.cs file has this within the <code>Application_Start()</code> method:</p>
  
  <p><code>Database.SetInitializer&lt;EfDbContext&gt;(new EfDbContextInitializer());</code></p>
  
  <p>Which looks like this:</p>
  
  <pre><code>public class EfDbContextInitializer : DropCreateDatabaseIfModelChanges&lt;EfDbContext&gt;
  {
      protected override void Seed(EfDbContext context)
      {
          var orders = new List&lt;Order&gt;
              {
                  . . .
              };
          orders.ForEach(s =&gt; context.Orders.Add(s));
           . . . etc. . .
          context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>My connection string from <code>Web.config</code>:</p>
  
  <p><code>&lt;add name="EFDbContext" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;database=pos;AttachDBFilename=|DataDirectory|pos.mdf;MultipleActiveResultSets=true;User Instance=true" providerName="System.Data.SqlClient" /&gt;</code></p>
  
  <p>And, finally, my Order and OrderDetails models (what the first error is directly referencing):</p>
  
  <pre><code>public class Order
  {
      public int OrderId { get; set; }
  
      public List&lt;OrderDetail&gt; OrderDetails { get; set; }
  
      public int EstablishmentId { get; set; }
  
      public virtual Establishment Establishment { get; set; }
  }
  
  public class OrderDetail
  {
      public int OrderDetailId { get; set; }
  
      public int OrderId { get; set; }
  
      public int ProductId { get; set; }
  
      public int Quantity { get; set; }
  
      public decimal UnitPrice { get; set; }
  
      public virtual Product Product { get; set; }
  
      public virtual Order Order { get; set; }
  }
  </code></pre>
  
  <p>Update / Note: If I comment out <code>public int OrderId { get; set; }</code> in my OrderDetail class, the project starts up fine (although I do not get the desired ability to add an <code>OrderId</code> (of course).</p>