From 076e8db5d964fc1c05381737d5679d9e5d048929 Mon Sep 17 00:00:00 2001 From: Bart Koelman <10324372+bkoelman@users.noreply.github.com> Date: Sat, 10 Oct 2026 02:43:51 +0200 Subject: [PATCH] Update API to JsonApiDotNetCore v5.12.0 --- TodoListAPI/Data/AppDbContext.cs | 20 ++++++++++++++++++++ TodoListAPI/Program.cs | 16 +++++++++++++++- TodoListAPI/TodoListAPI.csproj | 2 +- TodoListAPI/appsettings.json | 3 +-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/TodoListAPI/Data/AppDbContext.cs b/TodoListAPI/Data/AppDbContext.cs index cd0d22b..f1e1f77 100644 --- a/TodoListAPI/Data/AppDbContext.cs +++ b/TodoListAPI/Data/AppDbContext.cs @@ -1,6 +1,7 @@ using System.Security.Claims; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; using OpenIddict.Abstractions; using TodoListAPI.Models; @@ -17,6 +18,8 @@ protected override void OnModelCreating(ModelBuilder builder) builder.Entity() .HasQueryFilter(todoItem => !IsInHttpRequest() || todoItem.Owner.Id == GetCurrentUserId()); + + AdjustDeleteBehaviorForJsonApi(builder); } private bool IsInHttpRequest() @@ -35,4 +38,21 @@ private string GetCurrentUserId() throw new InvalidOperationException("Could not find current user."); } + + private static void AdjustDeleteBehaviorForJsonApi(ModelBuilder builder) + { + foreach (IMutableForeignKey foreignKey in builder.Model.GetEntityTypes() + .SelectMany(entityType => entityType.GetForeignKeys())) + { + if (foreignKey.DeleteBehavior == DeleteBehavior.ClientSetNull) + { + foreignKey.DeleteBehavior = DeleteBehavior.SetNull; + } + + if (foreignKey.DeleteBehavior == DeleteBehavior.ClientCascade) + { + foreignKey.DeleteBehavior = DeleteBehavior.Cascade; + } + } + } } diff --git a/TodoListAPI/Program.cs b/TodoListAPI/Program.cs index 0e5e132..32e50e6 100644 --- a/TodoListAPI/Program.cs +++ b/TodoListAPI/Program.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; +using OpenIddict.EntityFrameworkCore.Models; using TodoListAPI.Data; using TodoListAPI.Models; @@ -43,7 +44,20 @@ options.IncludeExceptionStackTraceInErrors = true; options.IncludeRequestBodyInErrors = true; #endif -}, discovery => discovery.AddCurrentAssembly()); +}, discovery => discovery.AddCurrentAssembly(), resourceGraphBuilder => +{ + resourceGraphBuilder + .SuppressWarningNotIdentifiable() + .SuppressWarningNotIdentifiable>() + .SuppressWarningNotIdentifiable>() + .SuppressWarningNotIdentifiable>() + .SuppressWarningNotIdentifiable>() + .SuppressWarningNotIdentifiable>() + .SuppressWarningNotIdentifiable() + .SuppressWarningNotIdentifiable() + .SuppressWarningNotIdentifiable() + .SuppressWarningNotIdentifiable(); +}); // Register the Identity services. builder.Services.AddIdentity() diff --git a/TodoListAPI/TodoListAPI.csproj b/TodoListAPI/TodoListAPI.csproj index e786b51..a18a648 100644 --- a/TodoListAPI/TodoListAPI.csproj +++ b/TodoListAPI/TodoListAPI.csproj @@ -7,7 +7,7 @@ - + diff --git a/TodoListAPI/appsettings.json b/TodoListAPI/appsettings.json index 472147e..6deb475 100755 --- a/TodoListAPI/appsettings.json +++ b/TodoListAPI/appsettings.json @@ -5,8 +5,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.EntityFrameworkCore.Database.Command": "Warning", - "JsonApiDotNetCore.Configuration.ResourceGraphBuilder": "Error" + "Microsoft.EntityFrameworkCore.Database.Command": "Warning" } }, "AllowedHosts": "*"