title: Entity Framework Core, one-to-many relationship, collection always empty
body: <p>I'm trying to do a simple one to many relationship in a InMemory database that is built up in runtime by http requests. I'm new to Entity Framework Core, so I'm unsure if the is a proper way of working with it.</p>
  
  <p>ValuesController.cs</p>
  
  <pre><code>[Route("api/[controller]")]
  [ApiController]
  public class ValuesController : ControllerBase
  {
      private readonly DataListContext _Context;
      public ValuesController(DataListContext context)
      {
          _Context = context;
      }
  
      [HttpGet]
      public ActionResult&lt;IEnumerable&lt;DataList&gt;&gt; Get()
      { 
          // Datas keeps is value
          Console.WriteLine("Root Count: " + _Context.RootDatas.Count());
          if (_Context.RootDataLists.Count() &gt; 0)
          {
              // InsideDatas is always 
              Console.WriteLine("Inside Count: " + _Context.RootDataLists.First().InsideDatas.Count);empty next request
          }
  
          return _Context.RootDataLists.ToList();
      }
  
      [HttpPost]
      public void Post([FromBody] string value)
      {
          var data = new Data() { Id = Guid.NewGuid() };
          _Context.RootDatas.Add(data);
  
          var dataList = new DataList();
          dataList.Name = value;
          dataList.InsideDatas.Add(data);
  
          _Context.RootDataLists.Add(dataList);
          _Context.SaveChanges();
  
          // InsideDatas has an element
          Console.WriteLine("Inside Count: " + _Context.RootDataLists.First().InsideDatas.Count);
      }
  }
  
  public class DataListContext : DbContext
  {
      public DbSet&lt;DataList&gt; RootDataLists { get; set; }
      public DbSet&lt;Data&gt; RootDatas { get; set; }
      public DataListContext(DbContextOptions&lt;DataListContext&gt; options) : base(options) { }
  }
  
  public class Data
  {
      [Key] public Guid Id { get; set; }
  }
  
  public class DataList
  {
      public DataList()
      {
          InsideDatas = new List&lt;Data&gt;();
      }
  
      [Key]
      public string Name { get; set; }
      public ICollection&lt;Data&gt; InsideDatas { get; set; }
  }
  </code></pre>
  
  <p>Startup.cs</p>
  
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext&lt;DataListContext&gt;(options =&gt; options.UseInMemoryDatabase(databaseName: "DataList"));
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseMvc();
      }
  }
  </code></pre>
  
  <p>My problem if that the InsideDatas list is always empty in the next call, but not RootDatas. The DbSet RootDatas in DataListContext is not really anything I would need in my application, but I though it would make the database keep the InsideDatas element. But no.</p>
  
  <p>So I'm I using this all wrong? My use case is a server listing, built up in runtime by hosts posting it's presence to the listing service. My plan is to swap the InMemoryDatabase for redis to be able to scale this properly if needed. (This code is just a slimmed version of the actual one with he same problem)</p>