Loop within the database

While working on my project (angular 8 + asp.net 3.0), I encountered a 500 error when attempting to retrieve all Products:

"System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32."

I'm not sure where the cycle in my code is occurring. Do I need to redesign all my models? Can someone please clarify what mistake I might be making? The goal was to create CRUD operations for Products, which could then be used to create Dishes. Eventually, users should be able to add products or dishes to their daily menu. Thank you for any assistance.

Here is my modelBuilder below:

modelBuilder.Entity<Ingredient>()
                .HasOne(x => x.Dish)
                .WithMany(x => x.Ingredients)
                .HasForeignKey(x => x.DishId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<Ingredient>()
                .HasOne(x => x.Product)
                .WithMany(x => x.Element)
                .HasForeignKey(x => x.ProductId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<Ingredient>()
                .HasKey(x => new { x.DishId, x.ProductId });

            modelBuilder.Entity<Product>()
                .HasOne(x => x.Photo)
                .WithMany()
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<Dish>()
                .HasOne(x => x.Photo)
                .WithMany()
                .OnDelete(DeleteBehavior.NoAction);

Database diagram

Answer №1

After encountering difficulties with configuration and models, the issue was resolved by including the following line of code. Special thanks to @bnu for the support.

services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to call a method from app.component in another component?

Having recently delved into Typescript and Angular 2, I've been struggling to find a solution online that fits my needs. Let's consider the example of an app.component: export class AppComponent implements OnInit { constructor(public _test ...

In C#, extract the text from the first column of a table and store it in a List<string> for verification purposes

I have a table containing uploaded files that I need to verify for successful uploading. Here is the screenshot of the table. Currently, my method iterates through the first column to add file names to a list for verification against an expected list. How ...

What is the best way to implement custom sorting for API response data in a mat-table?

I have been experimenting with implementing custom sorting in a mat-table using API response data. Unfortunately, I have not been able to achieve the desired result. Take a look at my Stackblitz Demo https://i.sstatic.net/UzK3p.png I attempted to implem ...

Declaring and accessing class variables in Angular 4

I am facing an issue with the following TypeScript model: export class User { email: string; token: string; username: string; bio: string; image: string; constructor() {} } When I attempt to instantiate this model in another TypeScript file, ...

Utilizing Angular HTTP Interceptor to Show Loading Spinner Across Multiple Modules

My goal is to utilize the ng4-loading-spinner spinner when making HTTP calls to my API. I referred to the examples provided in the following resources: Angular Guide on Intercepting HTTP Requests/Responses Stack Overflow Post on Using HttpClient Interce ...

Tips for transferring several parameters from an Angular 8 application to a .NET Core API

Hello, I am facing an issue with passing multiple string parameters from an Angular 8 app to a .NET Core API. This is my first time posting a question so I will try to provide all the necessary details. Here is the Angular code snippet: import { Injectab ...

Error message: "The property is not found within the specified type when using the OR operator with

Within my Angular component, I am faced with a challenge involving an Input that can be one of two types. @Input() profile: UserProfileDetails | BusinessProfileDetails; The structure of the profile template is straightforward and I want to avoid duplicati ...

Creating a deep copy of a bitmap while modifying its PixelFormat

Initializing a Bitmap image from the file "C:\\temp\\images\\file.jpg". The PixelFormat of the image is identified as Format24bppRgb. However, I noticed that when attempting to perform a deep copy... Another Bitmap img2 is ...

Discover the reasoning behind combining the two td elements

https://i.sstatic.net/WJx82.png What is the best way to create an XPath that can confirm 'Aetna' and 'passed' even if there are two 'passed' coverages distinguished by their names? https://i.sstatic.net/QRi65.png How can I ...

What is the best method for launching a Node.js (Express) app on a live server automatically?

My Angular app relies on an express backend. What is the best way to deploy this application on a remote server so that it always runs smoothly? ...

Issue with clearing subscription upon navigating to another page is not functioning as expected

Currently, I am working on building a basic search screen to gain a better understanding of Angular subscriptions, which I have found to be quite perplexing. On my home page, I have set up two components - one for filtering and the other for displaying sea ...

Is there a way in Typescript to determine the type of a union type when a specific condition is satisfied?

I am faced with a scenario where I have an object that can take on two different shapes: {ageTop:42} or {locations: [{name:Myrtle Beach}]} These objects are passed as parameters to a function, and I want to ensure that the function only receives the t ...

Sending a complex JavaScript object to the MVC controller with a consistent 400 response code

Once again facing another MVC issue. Someday, I'll look back at my "MVC learning days" and smile. So here's the problem - a constant 400 response. Despite reading numerous posts on SO, I just can't seem to get my logic working. Maybe if I s ...

Spring Boot app experiences issues with certain endpoints not functioning properly upon receiving requests from the Angular front end

My code includes the following endpoint: @PutMapping(path = "/like/{id}") public void likeQuestion(@PathVariable("id") String id, @Valid @NotNull @RequestBody Student student) { logger.info(id, student); questionService.likeQues ...

NextJS: Route Handler encountering Method Not Allowed (405) error when trying to redirect

Current NextJs version is 13.4.3 I have set up a route handler to capture POST requests. For more information on route handlers, please refer to the documentation at [https://nextjs.org/docs/app/building-your-application/routing/router-handlers] In this ...

Managing errors with promises in Angular 6 using SSH2

Attempting to manage ssh2 error messages using Angular has been a bit challenging for me. I tried implementing a promise to handle it, but unfortunately, it's not working as expected. Being new to this, I apologize if my approach is inadequate, and I& ...

Mistakes following the modification of tsconfig.json and package.json

Recently, I delved into exploring the Ahead-of-time compilation cookbook for Angular and found it quite intriguing. However, it seems like the errors I am encountering are not directly related to my new venture. You can check out the cookbook here: https:/ ...

What is the mechanism by which a ComboBox seizes control of the mouse when in the dropped-down

Exploring how to simulate the behavior of a ComboBox dropdown, as well as other types of drop downs like context menus, to ensure they close when clicking elsewhere - even if it is not an item that can be focused. In my attempts, I have experimented with ...

Inquiring about the application of spread argument in TypeScript

Here is some code I'm working on: import _ from 'lodash'; function test(num1: number, num2: number) { console.log(num1, num2); } test(..._.take(_.shuffle([0, 1, 2]), 2)); I encountered a TS2556 error while using the TS playground and ...

Generate a distinct variable name within the *ngFor loop

I am trying to create a dynamic table where clicking a button displays the row directly beneath it. I checked out a helpful post on this topic, but didn't find the exact solution I needed. The current setup works but reveals all hidden rows because ...