title: ASP.NET 3.1 Web API, Entity Framework one to many relationship either not made or ending in cycle
body: <p>So I am trying to write a server with a database which would host a site similar to reddit: with users, groups, posts and comments.</p>
  
  <p>As this is my first time developing backend, I tried googling and found ASP.NET Core 3.1 Web API with Entity Framework to be "good". (I use SQL Server as the database server if that's relevant)</p>
  
  <p>Now I went with the code first method, and created the tables with a migration.
  (<a href="https://i.stack.imgur.com/kXECE.png" rel="nofollow noreferrer">ERD</a>)</p>
  
  <p>I used the built in tool to create the controllers.</p>
  
  <p>If I used this JSON to create a post entity (with HTTP POST), the AuthorId and the LocationId stay on NULL (weather I use "" for the numbers or not). I created the Group and the User it would refer to earlier.</p>
  
  <p>The JSON:</p>
  
  <pre><code>{
      "title": "Post",
      "posttext": "text",
      "creatorid": "1",
      "locationid": "1",
      "timeofpost": "2020-01-12"
  }
  </code></pre>
  
  <p>The Post class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>
      using System;
      using System.Collections.Generic;
  
      namespace CedditBackend.Models
      {
          public class Post
          {
              public int Id { get; set; }
              public string Title { get; set; }
      #nullable enable
              public string? PostText { get; set; }
              public byte[]? Content { get; set; }
              public ICollection&lt;Comment&gt;? Comments { get; set; }
      #nullable disable
              public User Creator { get; set; }
              public Group Location { get; set; }
              public DateTime TimeOfPost { get; set; }
  
          }
      }
  
  </code></pre>
  
  <p>The autogenerated HTTPPOST in the controller:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    [HttpPost]
              public async Task&lt;ActionResult&lt;Post&gt;&gt; PostPost(Post post)
              {
                  _context.Posts.Add(post);
  
                  await _context.SaveChangesAsync();
  
                  return CreatedAtAction("GetPost", new { id = post.Id }, post);
              }
  </code></pre>
  
  <p>On the other hand if I try and create my own post class from the JSON (basically deserializing it), I get an error that "a cycle was detected". This I tried googling, but only found that with an earlier version (I guess) I could just turn that off.</p>
  
  <p>Still if I open the database, it shows me that the values of LocationId and AuthorId are not NULL, but if I try to get the posts of the Users (with an HTTP GET), all I get is the same error message. </p>
  
  <p>My rewritten HTTPPOST</p>
  
  <pre class="lang-cs prettyprint-override"><code>    [HttpPost]
              public async Task&lt;ActionResult&lt;Post&gt;&gt; PostPost(Object obj)
              {
                  Dictionary&lt;string, object&gt; dict = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(obj.ToString());
  
                  Post post = new Post();
                  post.Title = dict["title"].ToString();
                  post.PostText = dict["posttext"].ToString();
                  post.TimeOfPost = DateTime.Parse(dict["timeofpost"].ToString());
                  post.Location = _context.Groups.FirstOrDefault(x =&gt; x.Id == int.Parse(dict["locationid"].ToString()));
                  post.Creator = _context.Users.FirstOrDefault(x =&gt; x.Id == int.Parse(dict["creatorid"].ToString()));
  
                  post.Creator.Posts.Add(post);
                  post.Location.Posts.Add(post);
  
                  _context.Posts.Add(post);
  
                  await _context.SaveChangesAsync();
  
                  return CreatedAtAction("GetPost", new { id = post.Id }, post);
              }
  </code></pre>
  
  <p>I tried to google this whole stuff in several ways, but found nothing. Am I missing something obvious?</p>
  
  <p>Edit: the User class (which I think causes the cycle reference) </p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections.Generic;
  
  namespace CedditBackend.Models
  {
  
          public class User
          {
              public int Id { get; set; }
              public string UserName { get; set; }
      #nullable enable
              public ICollection&lt;Post&gt;? Posts { get; set; } = new List&lt;Post&gt;();
              public ICollection&lt;Comment&gt;? Comments { get; set; } = new List&lt;Comment&gt;();
              public ICollection&lt;UserGroup&gt;? UserGroups { get; set; }
      #nullable disable
  
          }
  
  }
  
  </code></pre>