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": "*"