title: C# FrameworkCore and MySQL: Cannot delete or update a parent row: a foreign key constraint fails
body: <p>Need some help here... I am copying code directly from one of my school classes, and they show the result in their code, but each time I run it, I get the following exception error:</p>
  
  <blockquote>
    <p>Exception has occurred:
    CLR/Microsoft.EntityFrameworkCore.DbUpdateException An unhandled
    exception of type 'Microsoft.EntityFrameworkCore.DbUpdateException'
    occurred in Microsoft.EntityFrameworkCore.dll: 'An error occurred
    while updating the entries. See the inner exception for details.' 
    Inner exceptions found, see $exception in variables window for more
    details.  Innermost exception      MySql.Data.MySqlClient.MySqlException
    : Cannot delete or update a parent row: a foreign key constraint fails
    (<code>movies</code>.<code>film_actor</code>, CONSTRAINT <code>film_actor_actor</code> FOREIGN KEY
    (<code>ActorId</code>) REFERENCES <code>actor</code> (<code>actorid</code>))    at
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()    at
    MySqlConnector.Core.ResultSet.d__1.MoveNext()
    in
    C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line
    44</p>
  </blockquote>
  
  <p>Here is the method being called:</p>
  
  <pre><code>internal static void DeleteItem()
  {
      Console.WriteLine("Delete an Actor");
  
      Console.WriteLine("Enter an Actor ID");
      var actorId = Console.ReadLine().ToInt();
  
      var actor = MoviesContext.Instance.Actors.SingleOrDefault(a =&gt; a.ActorId == actorId);
      if (actor == null) {
          Console.WriteLine($"Actor with id {actorId} not found.");
      }
      else {
          Console.WriteLine("Existing Actors");
          WriteActors();
  
          MoviesContext.Instance.Actors.Remove(actor);
  
          MoviesContext.Instance.SaveChanges();
  
          Console.WriteLine("With actor removed.");
          WriteActors();
      }
  }
  </code></pre>
  
  <p>I have checked my code over and over again and it matches the instructor's code perfectly. Unfortunately, no one is addressing my question through the college Blackboard, so I brought my question here.</p>
  
  <p>The framework reverse-engineered the database to build all of the scaffolding for the code. The program is throwing this exception when the MySQL statement for <code>SaveChanges()</code> is sent.</p>
  
  <p>However, it must be from this table, because <code>SaveChanges()</code> is used in an earlier part of the code for something else, and it doesn't throw any errors. </p>
  
  <p>Ideas? This is driving me Looney! Thanks. :)</p>
  
  <pre>
  Using:
  
  C# 
  ConsoleTables 
  EntityFrameworkCore 
   + .Design 
   + .Tools         // Used as a DotNetCliToolReference 
   + .Tools.DotNet  // Used as a DotNetCliToolReference 
   + .MySql 
   + .MySql.Design 
  </pre>
  
  <p>I don't know anything about constraints, but here is the section of code I found that the framework generated that may be a contributor:</p>
  
  <pre><code>modelBuilder.Entity&lt;FilmActor&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; new { e.FilmId, e.ActorId });
  
                  entity.ToTable("film_actor");
  
                  entity.HasIndex(e =&gt; e.ActorId)
                      .HasName("film_actor_actor_idx");
  
                  entity.Property(e =&gt; e.FilmId).HasColumnType("int(11)");
  
                  entity.Property(e =&gt; e.ActorId).HasColumnType("int(11)");
  
                  entity.HasOne(d =&gt; d.Actor)
                      .WithMany(p =&gt; p.FilmActor)
                      .HasForeignKey(d =&gt; d.ActorId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("film_actor_actor");
  
                  entity.HasOne(d =&gt; d.Film)
                      .WithMany(p =&gt; p.FilmActor)
                      .HasForeignKey(d =&gt; d.FilmId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("film_actor_film");
              });
  </code></pre>
  
  <p>Perhaps I'm not using this model correctly somewhere?</p>
  
  <p><strong>RESOLUTION</strong></p>
  
  <p>After much help below I was able to accomplish the fix by simply creating a loop that went through the associated table <strong>film_actor</strong> and deleted any rows with the <strong>ActorId</strong> that was related to the actor being deleted. Here is the quick loop I put in before the <code>SaveChanges()</code> method gets executed:</p>
  
  <pre><code>foreach (var tatertot in MoviesContext.Instance.FilmActors) {
       if(tatertot.ActorId == actorId) {
            MoviesContext.Instance.FilmActors.Remove(tatertot);
       }
  }
  </code></pre>
  
  <p>I'd like to thank everyone for their help. I'm sure I'll have more as this class goes on! Thank you. :)</p>