title: odata: How do I expand an nested ICollection of items using $expand in Odata using .net core 3.1
body: <p>OData url:
  https://{{baseUrl}}/api/vehicles?$expand=OwnershipHistories</p>
  <p>I want to return a vehicle(s) and than $expand the OwnershipHistories as a List of owner ship histories.</p>
  <p>But I only get one (1) Ownershiphistorie.
  Question: <strong>Why can I not get all the ownership histories???</strong></p>
  <p>I have this on my controller:</p>
  <pre><code>        [HttpGet]
          [EnableQuery(PageSize = 10, MaxExpansionDepth = 20)]
          [ODataRoute(&quot;vehicles&quot;)]
  </code></pre>
  <p>I use this url: {{baseUrl}}/api/vehicles?$expand=OwnershipHistories</p>
  <p>I use the following c# code ():</p>
  <pre><code>public async Task&lt;IQueryable&lt;VehicleEntity&gt;&gt; GetVehicles()
  {
      var vehicles = _context.Vehicles;  // .Include(v =&gt; v.OwnershipHistories) =&gt; ??? Is not working...
  
      return vehicles;
  }
  </code></pre>
  <p>The result is:</p>
  <pre><code>{
      &quot;@odata.context&quot;: &quot;https://localhost:5001/api/$metadata#Vehicles(OwnershipHistories())&quot;,
      &quot;value&quot;: [
          {
              &quot;id&quot;: 1,
              &quot;registrationNumber&quot;: &quot;10STNV&quot;,
              &quot;vehicleIdentificationNumber&quot;: &quot;JF1BP9LLA6G052053&quot;,
              // more fields
              &quot;**OwnershipHistories**&quot;: [ ?????? Only **one** record, not the **8** records I expect  ?????)
                  {
                      &quot;VweID&quot;: 1,
                      &quot;registrationNumber&quot;: &quot;10STNV&quot;,
                      // more fields
                  }
              ]
          },
  }
  </code></pre>
  <p>This is the query for the database:</p>
  <p>I have two entities and they are views in the sql database:</p>
  <pre><code>OwnershipHistoryEntity:
      [Table(&quot;EigenaarsHistorie&quot;, Schema = &quot;Voertuig&quot;)]
      public class OwnershipHistoryEntity
      {
          [Key]
          public int VweID { get; set; } // VDNI
          // more fields
          public virtual VehicleEntity Vehicle { get; set; }
      }
  </code></pre>
  <p>VehicleEntity:</p>
  <pre><code>namespace VWE.MijnVWE.Vehicle.Api.DAL.Entities
  {
      [Table(&quot;VoertuigInformatie&quot;, Schema = &quot;Voertuig&quot;)]
      public class VehicleEntity
      {
          [Key]
          public int VweID { get; set; } // VDNI
          public string Kenteken { get; set; }
          public string VoertuigIdentificatieNummer { get; set; }
          // more fields
          [ForeignKey(&quot;VweID&quot;)]
          public ICollection&lt;OwnershipHistoryEntity&gt; OwnershipHistories { get; set; } = new List&lt;OwnershipHistoryEntity&gt;();
      }
  }
  </code></pre>
  <p>EdmModel builder:</p>
  <pre><code>using Microsoft.AspNet.OData.Builder;
  using Microsoft.OData.Edm;
  using VWE.MijnVWE.Vehicle.Api.DAL.Entities;
  
  namespace VWE.MijnVWE.Vehicle.Api.BLL.Builders
  {
      public static class ModelBuilder
      {
          private static IEdmModel _edmModel;
  
          public static IEdmModel GetEdmModel()
          {
              return GetExplicitEdmModel();
          }
  
          static IEdmModel GetExplicitEdmModel()
          {
              if (_edmModel == null)
              {
                  var modelBuilder = new ODataConventionModelBuilder();
  
                  var vehicles = modelBuilder.EntitySet&lt;VehicleEntity&gt;(&quot;Vehicles&quot;);
                  var ownershipHistories = modelBuilder.EntitySet&lt;OwnershipHistoryEntity&gt;(&quot;OwnershipHistories&quot;);
  
                  modelBuilder.Namespace = &quot;vwe.mijnvwe.vehicle&quot;;
                  modelBuilder.ContainerName = &quot;vehicleApiContainer&quot;;
  
                  vehicles.EntityType.Name = &quot;vehicles&quot;;
                  vehicles.EntityType.HasKey(k =&gt; k.VweID);
                  vehicles.EntityType.HasMany(v =&gt; v.OwnershipHistories);
                  vehicles.HasManyBinding(v =&gt; v.OwnershipHistories, &quot;OwnershipHistories&quot;);
  
                  ownershipHistories.EntityType.Name = &quot;ownershipHistories&quot;;
                  ownershipHistories.EntityType.HasKey(k =&gt; k.VweID);
                  ownershipHistories.EntityType.HasRequired(o =&gt; o.Vehicle, (o, t) =&gt; o.VweID == t.VweID);
  
                  vehicles.EntityType.Property(p =&gt; p.VweID).Name = &quot;id&quot;;
                  vehicles.EntityType.Property(p =&gt; p.Kenteken).Name = &quot;registrationNumber&quot;;
                  vehicles.EntityType.Property(p =&gt; p.VoertuigIdentificatieNummer).Name = &quot;vehicleIdentificationNumber&quot;;
  // more fields
                  ownershipHistories.EntityType.Property(p =&gt; p.Kenteken).Name = &quot;registrationNumber&quot;;
                  ownershipHistories.EntityType.Property(p =&gt; p.EventDatum).Name = &quot;eventDate&quot;;
                  ownershipHistories.EntityType.Property(p =&gt; p.SoortAansprReferentieCode).Name = &quot;liabilityReferenceCode&quot;;
                  ownershipHistories.EntityType.Property(p =&gt; p.RegistratieDatumAansprakelijkheid).Name = &quot;from&quot;;
                  ownershipHistories.EntityType.Property(p =&gt; p.RegistratieDatumAansprakelijkheidTot).Name = &quot;to&quot;;
                  ownershipHistories.EntityType.Property(p =&gt; p.DagenInBezit).Name = &quot;numberOfDays&quot;;
                  ownershipHistories.EntityType.Property(p =&gt; p.DatumGewijzigd).Name = &quot;changedDateTime&quot;;
  
                  _edmModel = modelBuilder.GetEdmModel();
  
              }
  
              return _edmModel;
          }
      }
  }
  </code></pre>
  <p>Ef core modelBuilder:</p>
  <pre><code>   public class VehicleDbContext : DbContext
      {
          public VehicleDbContext(DbContextOptions&lt;VehicleDbContext&gt; options)
              : base(options)
          { }
  
          public DbSet&lt;VehicleEntity&gt; Vehicles { get; set; }
          public DbSet&lt;OwnershipHistoryEntity&gt; OwnershipHistories { get; set; }
  // more fields
          protected override void OnModelCreating(ModelBuilder builder)
          {
  // ...
              base.OnModelCreating(builder);
          }
      }
  </code></pre>
  <p>This is the Select query:</p>
  <pre><code>
  SELECT [t].[VweID], [t].[AandrijvingOmschrijving], 
  //more fields.. [t0].[SoortAansprReferentieCode], [t0].[c0]
  FROM (
      SELECT TOP(@__TypedProperty_3) [v].[VweID], [v].[AandrijvingOmschrijving], [v].[AantalAangedrevenAssen], 
  // more fields 
  [v].[Werkingsbeginsel], [v].[Wielbasis]
      FROM [Voertuig].[VoertuigInformatie] AS [v]
      ORDER BY [v].[VweID]
  ) AS [t]
  OUTER APPLY (
      SELECT TOP(@__TypedProperty_1) @__TypedProperty_2 AS [c], [e].[VweID], [e].[DagenInBezit], [e].[DatumGewijzigd], [e].[EventDatum], [e].[Kenteken], [e].[RegistratieDatumAansprakelijkheid], [e].[RegistratieDatumAansprakelijkheidTot], [e].[SoortAansprReferentieCode], CAST(1 AS bit) AS [c0]
      FROM [Voertuig].[EigenaarsHistorie] AS [e]
      WHERE [t].[VweID] = [e].[VweID]
      ORDER BY [e].[VweID]
  ) AS [t0]
  ORDER BY [t].[VweID], [t0].[VweID]
  
  </code></pre>
  <p>It is quit similar to the following question:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/67198810/how-do-i-expand-an-nested-odata-table">Same question about washington school odata question</a></li>
  <li><a href="https://github.com/hassanhabib/ODataDemo" rel="nofollow noreferrer">Washington school code odata example</a></li>
  </ul>