title: SqlException: On Delete Cascade not working ASP.NET
body: <p>When I try to delete a user from the ASP.NETUsers table I get SqlException:</p>
  <blockquote>
  <p>SqlException: The DELETE statement conflicted with the REFERENCE
  constraint &quot;FK_Applications_AspNetUsers_UserID&quot;. The conflict occurred
  in database &quot;JobGuide&quot;, table &quot;dbo.Applications&quot;, column 'UserID'.</p>
  </blockquote>
  <p>This problem is occurring because the User's Id is the Foreign key in another table, but &quot;On delete cascade&quot; is not working. For more details this is my model:</p>
  <p>My extended Identity User:</p>
  <pre><code>public class AppUser : IdentityUser
  {
      public string Name { get; set; }
      public string City { get; set; }
      public string RoleName { get; set; }
  
      public virtual ICollection&lt;Application&gt; Applications { get; set; }
  }
  </code></pre>
  <p>Application model (i.e. when a user applies for a job):</p>
  <pre><code>public class Application
  {
      public int ApplicationID { get; set; }
  
      public int JobID { get; set; }
      public virtual Job Job { get; set; }
      
      public string UserID { get; set; }
      public virtual AppUser User { get; set; }
  }
  </code></pre>
  <p>Job model:</p>
  <pre><code>public class Job
  {
      public int JobID { get; set; }
      public string Title { get; set; }
  
      public virtual ICollection&lt;Application&gt; Applications { get; set; }
  }
  </code></pre>
  <p>So up to here I created two <strong>One to Many</strong> relationships, AspNetUser one to many with Application and Job one to many with Application.</p>
  <p>And this is my Fluent API mapping configuration:</p>
  <pre><code>protected override void OnModelCreating(ModelBuilder builder)
  {
      base.OnModelCreating(builder);
  
      builder.Entity&lt;Application&gt;()
          .HasKey(i =&gt; i.ApplicationID);
  
      builder.Entity&lt;Application&gt;()
             .HasOne&lt;AppUser&gt;(sc =&gt; sc.User)
             .WithMany(s =&gt; s.Applications)
             .HasForeignKey(sc =&gt; sc.UserID)
             .OnDelete(DeleteBehavior.Cascade);
  
      builder.Entity&lt;Application&gt;()
             .HasOne&lt;Job&gt;(sc =&gt; sc.Job)
             .WithMany(s =&gt; s.Applications)
             .HasForeignKey(sc =&gt; sc.JobID)
             .OnDelete(DeleteBehavior.Cascade);
  }
  </code></pre>
  <p>Delete method from controller:</p>
  <pre><code>    var userInfo = await userManager.FindByIdAsync(user.Id);
      if (userInfo == null) 
      {
          return NotFound();
      }
      _ = await userManager.RemoveFromRoleAsync(userInfo, userInfo.RoleName);
      _ = await userManager.DeleteAsync(userInfo);
      
      int rowsAffected = await db.SaveChangesAsync();
  </code></pre>
  <p>Any idea why this error is not disappearing, is Fluent API good? or i need to type raw Sql to delete the Application with that User once and then the User? I have looked at almost all similar questions but none of them are working for me.</p>