C# odata:如何使用.NETCore3.1在odata中使用$expand扩展嵌套的ICollection项

C# odata:如何使用.NETCore3.1在odata中使用$expand扩展嵌套的ICollection项,c#,entity-framework-core,odata,linq-to-entities,expand,C#,Entity Framework Core,Odata,Linq To Entities,Expand,ODataURL: https://{{baseUrl}}/api/vehicles?$expand=所有者历史 我想退回一辆车,并将车主历史记录扩展为车主历史记录列表 但我只有一(1)份所有权历史记录。 问题:为什么我不能获得所有的所有权历史记录??? 我的控制器上有这个: [HttpGet] [EnableQuery(PageSize = 10, MaxExpansionDepth = 20)] [ODataRoute("vehicl

ODataURL: https://{{baseUrl}}/api/vehicles?$expand=所有者历史

我想退回一辆车,并将车主历史记录扩展为车主历史记录列表

但我只有一(1)份所有权历史记录。 问题:为什么我不能获得所有的所有权历史记录???

我的控制器上有这个:

        [HttpGet]
        [EnableQuery(PageSize = 10, MaxExpansionDepth = 20)]
        [ODataRoute("vehicles")]
我使用这个url:{{baseUrl}}/api/vehicles?$expand=OwnershipHistories

我使用以下c代码():

这是对数据库的查询:

OwnershipHistoryEntity:
    [Table("EigenaarsHistorie", Schema = "Voertuig")]
    public class OwnershipHistoryEntity
    {
        [Key]
        public int VweID { get; set; } // VDNI
        // more fields
        public virtual VehicleEntity Vehicle { get; set; }
    }
我有两个实体,它们是sql数据库中的视图:

OwnershipHistoryEntity:
    [Table("EigenaarsHistorie", Schema = "Voertuig")]
    public class OwnershipHistoryEntity
    {
        [Key]
        public int VweID { get; set; } // VDNI
        // more fields
        public virtual VehicleEntity Vehicle { get; set; }
    }
车辆重量:

namespace VWE.MijnVWE.Vehicle.Api.DAL.Entities
{
    [Table("VoertuigInformatie", Schema = "Voertuig")]
    public class VehicleEntity
    {
        [Key]
        public int VweID { get; set; } // VDNI
        public string Kenteken { get; set; }
        public string VoertuigIdentificatieNummer { get; set; }
        // more fields
        [ForeignKey("VweID")]
        public ICollection<OwnershipHistoryEntity> OwnershipHistories { get; set; } = new List<OwnershipHistoryEntity>();
    }
}
这与下面的问题类似:


示例()学校有一批学生作为最高级别的回应。您没有顶级车辆集合。感谢您的回复。我可以在项目中做些什么来获得学校/$学生。然后也许我可以解决我自己的问题。但这是不可能的学校/$expand=学生。学生是空的。所以我不知道你的回答能帮我什么忙。还是我弄错了?为什么车辆不是顶级的?您使用两种方法1)实体,它使用dbContext 2)控制器,它解析从服务器接收到的http响应。我认为您要做的是在服务器上查询数据库,获取dbContext,然后使用控制器在响应中将dbContext发送给客户机。看起来数据库的查询将进入以下行:publicdbset车辆{get;set;}。然后,您必须在post中将车辆列表发送给客户。
namespace VWE.MijnVWE.Vehicle.Api.DAL.Entities
{
    [Table("VoertuigInformatie", Schema = "Voertuig")]
    public class VehicleEntity
    {
        [Key]
        public int VweID { get; set; } // VDNI
        public string Kenteken { get; set; }
        public string VoertuigIdentificatieNummer { get; set; }
        // more fields
        [ForeignKey("VweID")]
        public ICollection<OwnershipHistoryEntity> OwnershipHistories { get; set; } = new List<OwnershipHistoryEntity>();
    }
}
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<VehicleEntity>("Vehicles");
                var ownershipHistories = modelBuilder.EntitySet<OwnershipHistoryEntity>("OwnershipHistories");

                modelBuilder.Namespace = "vwe.mijnvwe.vehicle";
                modelBuilder.ContainerName = "vehicleApiContainer";

                vehicles.EntityType.Name = "vehicles";
                vehicles.EntityType.HasKey(k => k.VweID);
                vehicles.EntityType.HasMany(v => v.OwnershipHistories);
                vehicles.HasManyBinding(v => v.OwnershipHistories, "OwnershipHistories");

                ownershipHistories.EntityType.Name = "ownershipHistories";
                ownershipHistories.EntityType.HasKey(k => k.VweID);
                ownershipHistories.EntityType.HasRequired(o => o.Vehicle, (o, t) => o.VweID == t.VweID);

                vehicles.EntityType.Property(p => p.VweID).Name = "id";
                vehicles.EntityType.Property(p => p.Kenteken).Name = "registrationNumber";
                vehicles.EntityType.Property(p => p.VoertuigIdentificatieNummer).Name = "vehicleIdentificationNumber";
// more fields
                ownershipHistories.EntityType.Property(p => p.Kenteken).Name = "registrationNumber";
                ownershipHistories.EntityType.Property(p => p.EventDatum).Name = "eventDate";
                ownershipHistories.EntityType.Property(p => p.SoortAansprReferentieCode).Name = "liabilityReferenceCode";
                ownershipHistories.EntityType.Property(p => p.RegistratieDatumAansprakelijkheid).Name = "from";
                ownershipHistories.EntityType.Property(p => p.RegistratieDatumAansprakelijkheidTot).Name = "to";
                ownershipHistories.EntityType.Property(p => p.DagenInBezit).Name = "numberOfDays";
                ownershipHistories.EntityType.Property(p => p.DatumGewijzigd).Name = "changedDateTime";

                _edmModel = modelBuilder.GetEdmModel();

            }

            return _edmModel;
        }
    }
}
   public class VehicleDbContext : DbContext
    {
        public VehicleDbContext(DbContextOptions<VehicleDbContext> options)
            : base(options)
        { }

        public DbSet<VehicleEntity> Vehicles { get; set; }
        public DbSet<OwnershipHistoryEntity> OwnershipHistories { get; set; }
// more fields
        protected override void OnModelCreating(ModelBuilder builder)
        {
// ...
            base.OnModelCreating(builder);
        }
    }

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]