title: Why data is not being added to database? Entity Framework, Blazor wasm
body: <p>I have 3 tables in my database: <code>Ingredient</code>, <code>IngredientRecipe</code> and <code>Recipe</code>. Adding data to <code>Ingredient</code> works correctly. <code>IngredientRecipe</code> is for making relation between <code>Ingredient</code> and <code>Recipe</code>. When I want to add a <code>Recipe</code> to database, nothing happens. No errors, no data in the database.</p>
  <p>My error message:</p>
  <pre><code>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 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
  </code></pre>
  <p><code>DbContext</code> class:</p>
  <pre><code>public class AppDbContext : DbContext
  {
      public AppDbContext(DbContextOptions options) : base(options)
      {
      }
  
      public DbSet&lt;Ingredient&gt; IngredientsTable { get; set; }
      public DbSet&lt;IngredientRecipe&gt; IngredientRecipesTable { get; set; }
      public DbSet&lt;Recipe&gt; RecipesTable { get; set; }
  }
  </code></pre>
  <p>Ingredient, IngredientRecipe and Recipe:</p>
  <pre><code>public class Ingredient : BaseEntity
  {
      public string Name { get; set; }
      public int Kcal { get; set; }
  
      public virtual ICollection&lt;IngredientRecipe&gt; IngredientRecipe { get; set; }
  }
  
  public class IngredientRecipe : BaseEntity
  {
      public int IngredientId { get; set; }
      public int RecipeId { get; set; }
      public int Amount { get; set; }
  
      public Ingredient Ingredient { get; set; }
      public Recipe Recipe { get; set; }
  }
  
  public class Recipe : BaseEntity
  {
      public string Name { get; set; }
  
      public List&lt;IngredientRecipe&gt; ListOfIngredients { get; set; }
  }
  </code></pre>
  <p>And here is my form code:</p>
  <pre><code>@page &quot;/recipes/create&quot;
  @inject IIngredientRepository ingredientRepository
  @inject IRecipeRepository recipeRepository
  @inject NavigationManager navManager
  
  &lt;div class=&quot;container col-12&quot;&gt;
      &lt;EditForm Model=&quot;Recipe&quot; OnValidSubmit=&quot;SaveRecipe&quot;&gt;
          &lt;button class=&quot;btn btn-primary&quot;&gt;Save recipe&lt;/button&gt;
          &lt;div class=&quot;col-4&quot;&gt;
              &lt;div class=&quot;form-control&quot;&gt;
                  &lt;InputText @bind-Value=&quot;Recipe.Name&quot; /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/EditForm&gt;
  &lt;/div&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-6&quot;&gt;
          &lt;TableTemplate Items=&quot;ListOfIngredients&quot;&gt;
              &lt;TableHeader&gt;
                  &lt;th&gt;Name&lt;/th&gt;
                  &lt;th&gt;Kcal&lt;/th&gt;
                  &lt;th&gt;Action&lt;/th&gt;
              &lt;/TableHeader&gt;
              &lt;RowTemplate Context=&quot;ingredient&quot;&gt;
                  &lt;td&gt;@ingredient.Name&lt;/td&gt;
                  &lt;td&gt;@ingredient.Kcal&lt;/td&gt;
                  &lt;td&gt;
                      &lt;button class=&quot;btn btn-primary&quot; @onclick=&quot;@(()=&gt;SetTheAmount(ingredient))&quot;&gt;Add&lt;/button&gt;
                  &lt;/td&gt;
              &lt;/RowTemplate&gt;
          &lt;/TableTemplate&gt;
      &lt;/div&gt;
      &lt;div class=&quot;col-6&quot;&gt;
          List of ingredients
          &lt;br /&gt;
          @if (ListOfIngredientAmounts.Any())
          {
              &lt;TableTemplate Items=&quot;ListOfIngredientAmounts&quot;&gt;
                  &lt;TableHeader&gt;
                      &lt;th&gt;Name&lt;/th&gt;
                      &lt;th&gt;Amount&lt;/th&gt;
                      &lt;th&gt;Action&lt;/th&gt;
                  &lt;/TableHeader&gt;
                  &lt;RowTemplate Context=&quot;ingredient&quot;&gt;
                      &lt;td&gt;@ingredient.Ingredient.Name&lt;/td&gt;
                      &lt;td&gt;@ingredient.Amount&lt;/td&gt;
                      &lt;td&gt;
                          &lt;button class=&quot;btn btn-primary&quot;&gt;Delete&lt;/button&gt;
                      &lt;/td&gt;
                  &lt;/RowTemplate&gt;
              &lt;/TableTemplate&gt;
          }
      &lt;/div&gt;
  &lt;/div&gt;
  
  @if (IngredientAmount.Ingredient != null)
  {
      &lt;p&gt;Ingredient Id: @IngredientAmount.IngredientId | Name: @IngredientAmount.Ingredient.Name | Amount: @IngredientAmount.Amount&lt;/p&gt;
      &lt;EditForm Model=&quot;IngredientAmount&quot; OnValidSubmit=&quot;AddIngredientToIngredientAmountList&quot;&gt;
          &lt;div class=&quot;form-group&quot;&gt;
              Value
              &lt;InputNumber @bind-Value=&quot;IngredientAmount.Amount&quot; /&gt;
          &lt;/div&gt;
          &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Add to list&lt;/button&gt;
      &lt;/EditForm&gt;
  }
  
  @code{
      public Recipe Recipe { get; set; } = new Recipe();
  
      public List&lt;Ingredient&gt; ListOfIngredients { get; set; } = new List&lt;Ingredient&gt;();
  
      public IngredientRecipe IngredientAmount { get; set; } = new IngredientRecipe();
  
      public List&lt;IngredientRecipe&gt; ListOfIngredientAmounts { get; set; } = new List&lt;IngredientRecipe&gt;();
  
      protected override async Task OnInitializedAsync()
      {
          ListOfIngredients = await ingredientRepository.GetListOfIngredients();
      }
  
      private void SetTheAmount(Ingredient ingredient)
      {
          IngredientAmount = new IngredientRecipe();
          IngredientAmount.IngredientId = ingredient.Id;
          IngredientAmount.Recipe = Recipe;
          IngredientAmount.RecipeId = Recipe.Id;
          IngredientAmount.Ingredient = ingredient;
      }
  
      private void AddIngredientToIngredientAmountList()
      {
          ListOfIngredientAmounts.Add(IngredientAmount);
          IngredientAmount = new IngredientRecipe();
      }
  
      private async void SaveRecipe()
      {
          if (ListOfIngredientAmounts.Any())
          {
              Recipe.ListOfIngredients = ListOfIngredientAmounts;
  
              await recipeRepository.CreateRecipe(Recipe);
  
              ListOfIngredientAmounts = new List&lt;IngredientRecipe&gt;();
              Recipe = new Recipe();
              navManager.NavigateTo(&quot;/recipes&quot;);
          }
      }
  }
  </code></pre>
  <p>And my repository and controller responsible for post:</p>
  <pre><code>public async Task CreateRecipe(Recipe recipe)
  {
          var response = await httpClient.PostAsJsonAsync(url, recipe);
  
          if (!response.IsSuccessStatusCode)
          {
              throw new ApplicationException(await response.Content.ReadAsStringAsync());
          }
  }
  
  
  [HttpPost]
  public async Task&lt;ActionResult&lt;int&gt;&gt; Post(Recipe recipe)
  {
      context.RecipesTable.Add(recipe);
      await context.SaveChangesAsync();
      return recipe.Id;
  }
  </code></pre>
  <p>And here is link to GitHub <a href="https://github.com/szymonJag/CookBook" rel="nofollow noreferrer">https://github.com/szymonJag/CookBook</a></p>