title: Introducing FOREIGN KEY may cause cycles or multiple cascade paths
body: <p>i am using Entity Framework with code first approach.</p>
  
  <p>In my <code>onModelCreating</code> I am creating my tables with keys and relationships (I am using Fluent API approach, not data annotations).</p>
  
  <p>But when I try to generate my model using <code>Update-Database</code> command I receive following error</p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint 'FK_customers.invoices_customers.billingCenters_billingCenterId' on table 'invoices' 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.</p>
  </blockquote>
  
  <p>I almost sure I do not have cycles... and I would not have problem if I had cascade path. It's what I would like to have instead!</p>
  
  <p>Following the model I am creating:</p>
  
  <pre><code>modelBuilder.Entity&lt;Customer&gt;()
      .ToTable("customers", schemaName)
      .HasKey(c =&gt; new { c.Code });
  
  modelBuilder.Entity&lt;BillingCenter&gt;()
      .ToTable("billingCenters", schemaName)
      .HasKey(bc =&gt; new { bc.Id });
  
  //1 Customer -&gt; N BillingCenters
  modelBuilder.Entity&lt;BillingCenter&gt;()
      .HasRequired(bc =&gt; bc.Customer)
      .WithMany(c =&gt; c.BillingCenters)
      .HasForeignKey(bc =&gt; bc.CustomerId);
  
  modelBuilder.Entity&lt;Invoice&gt;()
      .ToTable("invoices", schemaName)
      .HasKey(i =&gt; new { i.Id });
  
  //Here the code gives me problems
  //1 BillingCenter -&gt; N Invoices
  modelBuilder.Entity&lt;Invoice&gt;()
      .HasRequired(i =&gt; i.BillingCenter)
      .WithMany(bc =&gt; bc.Invoices)
      .HasForeignKey(i =&gt; i.BillingCenterId);
  
  modelBuilder.Entity&lt;Payment&gt;()
      .ToTable("payments", schemaName)
      .HasKey(ep =&gt; new { ep.Id })
      .Property(ep =&gt; ep.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
  //1 Customer -&gt; N Payments
  modelBuilder.Entity&lt;Payment&gt;()
      .HasRequired(ep =&gt; ep.customer)
      .WithMany(c =&gt; c.Payments)
      .HasForeignKey(ep =&gt; ep.customerCode);
  
  //1 Invoice -&gt; N Payments (Failed, Ok, ...)
  modelBuilder.Entity&lt;Payment&gt;()
      .HasRequired(p =&gt; p.Invoice)
      .WithMany(i =&gt; i.Payments)
      .HasForeignKey(p =&gt; p.InvoiceId);
  </code></pre>
  
  <p>If I remove this code everything seems to work</p>
  
  <pre><code>modelBuilder.Entity&lt;Invoice&gt;()
      .HasRequired(i =&gt; i.BillingCenter)
      .WithMany(bc =&gt; bc.Invoices)
      .HasForeignKey(i =&gt; i.BillingCenterId);
  </code></pre>
  
  <p>And the following database is generated:</p>
  
  <p><a href="https://i.stack.imgur.com/iuDb1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iuDb1.png" alt="enter image description here"></a></p>
  
  <p>I say that it SEEMS to work because if I see the relation between <code>billingCenters</code> and <code>invoices</code> the <code>delete rule</code> is <code>no action</code>.</p>
  
  <p>How Can I solve the problem? </p>
  
  <p>Thank you in advance</p>