title: SqlException: The DELETE statement conflicted with the REFERENCE constraint, can't seem to get cascade delete to work
body: <p>I am getting this error:</p>
  <p>SqlException: The DELETE statement conflicted with the REFERENCE constraint &quot;FK_Games_AspNetUsers_VideoGamesUserId&quot;. The conflict occurred in database &quot;VideoGames&quot;, table &quot;dbo.Games&quot;, column 'VideoGamesUserId'.
  The statement has been terminated.</p>
  <p>when trying to remove an instance of VideoGamesUser from my database.</p>
  <pre><code>  public class VideoGamesUser : IdentityUser
  {
      public List&lt;Game&gt; UserGameLibrary { get; set; }
  
      public VideoGamesUser(List&lt;Game&gt; games)
      {
          UserGameLibrary = games;
      }
  
      public VideoGamesUser()
      {
  
      }
  }
  </code></pre>
  <p>And here is the Game class as well</p>
  <pre><code> public class Game
  {
      public long GameId { get; set; }
      [Required]
      public string Name { get; set; }
      public string Genre { get; set; }
      public bool Completed { get; set; }
      public VideoGamesUser VideoGamesUser { get; set; }
      
  
      public Game(string name, string genre, bool completed, int id)
      {
          GameId = id;
          Name = name;
          Genre = genre;
          Completed = completed;
  
      }
  
      public Game()
      {
         
  
      }
  
  }
  </code></pre>
  <p>I've searched through related questions and I believe the issue is because the Games table</p>
  <pre><code>CREATE TABLE [dbo].[Games] (
  [GameId]           BIGINT         IDENTITY (1, 1) NOT NULL,
  [Name]             NVARCHAR (MAX) NOT NULL,
  [Genre]            NVARCHAR (MAX) NULL,
  [Completed]        BIT            NOT NULL,
  [VideoGamesUserId] NVARCHAR (450) NULL,
  CONSTRAINT [PK_Games] PRIMARY KEY CLUSTERED ([GameId] ASC),
  CONSTRAINT [FK_Games_AspNetUsers_VideoGamesUserId] FOREIGN KEY ([VideoGamesUserId]) REFERENCES [dbo].[AspNetUsers] ([Id])
  </code></pre>
  <p>is referencing the VideoGamesUser class via a foreign key and thus I cannot delete the VideoGamesUser without first deleting the content of the Games table. But to my understanding, cascade delete should enable me to remove both at the same time.</p>
  <p>So my issue is even after following this</p>
  <p><a href="https://www.tektutorialshub.com/entity-framework-core/cascade-delete-in-entity-framework-core/" rel="nofollow noreferrer">https://www.tektutorialshub.com/entity-framework-core/cascade-delete-in-entity-framework-core/</a></p>
  <p>the resulting OnModelCreating method from my dbcontext class:</p>
  <pre><code>    protected override void OnModelCreating(ModelBuilder builder)
      {
          base.OnModelCreating(builder);
  
          builder.Entity&lt;Game&gt;()
              .HasOne&lt;VideoGamesUser&gt;(u =&gt; u.VideoGamesUser)
              .WithMany(g =&gt; g.UserGameLibrary)
              .HasForeignKey(&quot;VideoGamesUserId&quot;)
              .IsRequired(true)
              .OnDelete(DeleteBehavior.Cascade);
  
      }
  }
  </code></pre>
  <p>I am still getting the error. I guess I am just not understanding something here. Any ideas or direction would be appreciated. Thanks.</p>
  <p>Edit:</p>
  <p>The delete method is the built in one provided when I scaffolded in ASP.Net Identity</p>
  <pre><code> public async Task&lt;IActionResult&gt; OnPostAsync()
      {
          var user = await _userManager.GetUserAsync(User);
          if (user == null)
          {
              return NotFound($&quot;Unable to load user with ID '{_userManager.GetUserId(User)}'.&quot;);
          }
  
          RequirePassword = await _userManager.HasPasswordAsync(user);
          if (RequirePassword)
          {
              if (!await _userManager.CheckPasswordAsync(user, Input.Password))
              {
                  ModelState.AddModelError(string.Empty, &quot;Incorrect password.&quot;);
                  return Page();
              }
          }
        
          var result = await _userManager.DeleteAsync(user);
          var userId = await _userManager.GetUserIdAsync(user);
          if (!result.Succeeded)
          {
              throw new InvalidOperationException($&quot;Unexpected error occurred deleting user with ID '{userId}'.&quot;);
          }
  
          await _signInManager.SignOutAsync();
  
          _logger.LogInformation(&quot;User with ID '{UserId}' deleted themselves.&quot;, userId);
  
          return Redirect(&quot;~/&quot;);
      }
  </code></pre>