title: How to make an insertion with objects inside objects?
body: <p>I'm trying to do Post method using entity framework.I have the objects:</p>
  
  <pre><code>public class Pedido:BaseModel
      {
          public Pedido()
          {
          }
  
          public Pedido(List&lt;PedidoItem&gt; itens, double valorTotal, DateTime tempoTotal)
          {
              Id = 0;
              Itens = itens;
              ValorTotal = valorTotal;
              TempoTotal = tempoTotal;
          }
  
          public Pedido(int id, List&lt;PedidoItem&gt; itens, double valorTotal, DateTime tempoTotal)
          {
              Id = id;
              Itens = itens;
              ValorTotal = valorTotal;
              TempoTotal = tempoTotal;
          }
  
          public double ValorTotal { get; set; }
          public DateTime TempoTotal { get; set; }
          public List&lt;PedidoItem&gt; Itens { get; set; }
  
          public override BaseModel WithId(int id)
          {
              throw new System.NotImplementedException();
          }
      }
  
  public class PedidoItem:BaseModel
      {
          public PedidoItem()
          {
          }
  
          public PedidoItem(int saborId, int tamanhoId, List&lt;PedidoItemAdicional&gt; adicionais)
          {
              Id = 0;
              SaborId = saborId;
              TamanhoId = tamanhoId;
              Adicionais = adicionais;
          }
  
          public PedidoItem(int id, int saborId, int tamanhoId, List&lt;PedidoItemAdicional&gt; adicionais)
          {
              Id = id;
              SaborId = saborId;
              TamanhoId = tamanhoId;
              Adicionais = adicionais;
          }
  
          [ForeignKey(nameof(Sabor))]
          public int SaborId { get; set; }
          public Sabor Sabor { get; set; }
          [ForeignKey(nameof(Tamanho))]
          public int TamanhoId { get; set; }
          public Tamanho Tamanho { get; set; }
  
          public List&lt;PedidoItemAdicional&gt; Adicionais;
          [ForeignKey(nameof(Pedido))]
          public int PedidoId { get; set; }
  
          public override BaseModel WithId(int id)
          {
              throw new System.NotImplementedException();
          }
      }
  
  public class PedidoItemAdicional:BaseModel
      {
  
          public PedidoItemAdicional()
          {
          }
  
          public PedidoItemAdicional(int adicionalId, int pedidoItemId)
          {
              Id = 0;
              AdicionalId = adicionalId;
              PedidoItemId = pedidoItemId;
          }
  
          public PedidoItemAdicional(int id, int adicionalId, int pedidoItemId)
          {
              Id = id;
              AdicionalId = adicionalId;
              PedidoItemId = pedidoItemId;
          }
  
          [ForeignKey(nameof(Adicional))]
          public int AdicionalId { get; set; }
          public Adicional Adicional { get; set; }
          [ForeignKey(nameof(PedidoItem))]
          public int PedidoItemId { get; set; }
          public PedidoItem PedidoItem { get; set; }
  
          public override BaseModel WithId(int id)
          {
              throw new NotImplementedException();
          }
      }
  
  public abstract class BaseModel
      {
  
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int Id { get;  set; }
          public abstract BaseModel WithId(int id);
      }
  </code></pre>
  
  <p>I am receiving a JSON:</p>
  
  <pre><code>{
      "itens":[
          {
              "tamanhoid":1,
              "saborid":1,
              "adicionais":[
                  {
                      "id":1
                  },
                  {
                      "id":2
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>When I insert in the bank (add + save) the "pedido" and "pedidoItem" are saved, but the "pedidoItemAdicional" is not.</p>
  
  <p>To make the insert I'm using a repository:</p>
  
  <pre><code>public class PedidoRepository:BaseRepository&lt;Pedido,DataContext&gt;
      {
          public PedidoRepository(DataContext context):base(context)
          {
  
          }
      }
  public abstract class BaseRepository&lt;TEntity, TContext&gt;
          where TEntity : BaseModel
          where TContext : DbContext
      {
          protected DbSet&lt;TEntity&gt; DbSet;
          protected TContext DbContext;
  
          protected BaseRepository(TContext dbContext)
          {
              DbContext = dbContext;
              DbSet = DbContext.Set&lt;TEntity&gt;();
          }
  
          public virtual async Task&lt;List&lt;TEntity&gt;&gt; Get
              (            
              Expression&lt;Func&lt;TEntity, object&gt;&gt; orderBy,
              bool asNoTracking = true,
              int take = 100
              )
          {
              var databaseCount = await DbSet.CountAsync().ConfigureAwait(false);
              if (asNoTracking)
                  return await DbSet.AsNoTracking().OrderBy(orderBy).Take(take).ToListAsync().ConfigureAwait(false);
  
  
              return await DbSet.OrderBy(orderBy).Take(take).ToListAsync().ConfigureAwait(false);
  
          }
  
          public virtual async Task&lt;List&lt;TEntity&gt;&gt; Get
          (
              Expression&lt;Func&lt;TEntity, object&gt;&gt; orderBy,
              Expression&lt;Func&lt;TEntity, bool&gt;&gt; where,
              bool asNoTracking = true,
              int take = 100
          )
          {
              if (asNoTracking)
                  return await DbSet.AsNoTracking().OrderBy(orderBy).Where(where).Take(take).ToListAsync()
                      .ConfigureAwait(false);
  
              return await DbSet.OrderBy(orderBy).Where(where).Take(take).ToListAsync()
               .ConfigureAwait(false);
  
          }
  
          public virtual async Task AddAsync(TEntity entity)
          {
              await DbSet.AddAsync(entity).ConfigureAwait(false);
          }
  
          public virtual Task UpdateAsync(TEntity entity)
          {
              DbSet.Update(entity);
              return Task.CompletedTask;
          }
  
          public virtual Task RemoveAsync(TEntity entity)
          {
              DbSet.Remove(entity);
              return Task.CompletedTask;
          }
  
          public virtual async Task SaveChangesAsync()
          {
              await DbContext.SaveChangesAsync().ConfigureAwait(false);
          }
      }
  
  
  [HttpPost]
          [Route("")]
          public async Task&lt;ActionResult&lt;Response&lt;Pedido&gt;&gt;&gt; Insert(Pedido pedido)
          {
              await _repositoryPedido.AddAsync(obj);
              await _repositoryPedido.SaveChangesAsync();
              return null;
          }
  </code></pre>
  
  <p>The reception is ok, the "pedidoItemAdicional" is received, but not saved ... I would like it to be saved as with "pedidoItem".</p>