0

I am trying to return the database object that I get from my service in an IActionResult API Call (c# web API project). Whenever I attempt to do that I get this error:

System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.

Here is my API code that is throwing this:

        [HttpGet]
        [Route("content")]
        public IActionResult GetAllContent()
        {
            try
            {
                List<Content> allContent = _contentService.GetAll();
                return Ok(allContent);
            }
            catch (Exception ex)
            {
               //Log something here
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }

I could easily mitigate the error by parsing through the content and creating a dynamic object, but I find it annoying to do whenever I want to return a database object when I'm using the Devart Database Context.

Edit: Further piece of the error message is this:

$.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.PortalCodeMappings.Content.ContentId.

And I understand the circular reference here, is there a way to tell devart I only want the Content section of this without doing something like this:

allContent.Select(x => new { ... });

Edit: I am using Devart Entity Devloper to generate my models and the dbcontext. I do not use Visual Studio or any IDE.

  • What is the value of `allContent`? The error message seems to have a good explanation of what's going on, do you agree with it? – gunr2171 Nov 23 '22 at 15:42
  • Okay good note! let me add some more details to the question, because those extra pieces are important. – austin_wilcox21 Nov 23 '22 at 15:53
  • Edit made to the original post @gunr2171 – austin_wilcox21 Nov 23 '22 at 15:56
  • Try setting Serialize Navigation Properties=None in the properties of the template used in your Devart Entity Model. Does this help in your scenario? – Devart Nov 25 '22 at 13:37
  • @Devart I will try that out on Monday when I get back to work. Thank you for the response. – austin_wilcox21 Nov 26 '22 at 14:04
  • @Devart I am not seeing any options in Entity Developer for Serialize Navigation Properties. I see several options for Serialize and the option to set it to none or unidirectional, but all those options are off by default. – austin_wilcox21 Jan 03 '23 at 20:01
  • You are working with EF Core Model, aren't you? Open your *.efml model, navigate to Tools > Entity Developer > Model Explorer > the Templates node, select the EF Core template, press F4 (Properties), and set Serialize Navigation Properties=None. – Devart Jan 09 '23 at 11:27
  • I should add the comment, I am doing all the model and context generation with the Devart Entity Developer tool. I do not use Visual Studio or any IDE for that matter. Are there steps to do all of this within Devart Entity Developer? – austin_wilcox21 Jan 09 '23 at 17:04
  • With a standalone Entity Developer tool, navigate to View > Model Explorer > the Templates node, select the EF Core template, press F4 (Properties), and set Serialize Navigation Properties=None. Save the model and press the Generate Code button (F7). – Devart Jan 11 '23 at 11:38
  • The only template I have is LinqConnect, am I supposed to have EF Core Template in there? – austin_wilcox21 Jan 11 '23 at 16:22
  • If you are working with LinqConnect ORM, your template is LinqConnect (not EF Core). Refer to our answer to your question. – Devart Jan 16 '23 at 15:14

1 Answers1

1

There are two alternative ways to solve the issue:

  1. Use System.Text.Json (de)serialization

  2. Add JsonIgnoreAttribute to one of the navigation property ends

You can add a custom attribute via the interface of Entity Developer this way:

a) navigate to Model > Settings > Attributes > select the System.Text.Json.dll assembly and make sure that JsonIgnoreAttribute is checked in the window below, press OK

b) select JsonIgnoreAttribute in the Attributes collection of a particular class property

Devart
  • 119,203
  • 23
  • 166
  • 186