C# odata:如何使用.NETCore3.1在odata中使用$expand扩展嵌套的ICollection项
ODataURL: https://{{baseUrl}}/api/vehicles?$expand=所有者历史 我想退回一辆车,并将车主历史记录扩展为车主历史记录列表 但我只有一(1)份所有权历史记录。 问题:为什么我不能获得所有的所有权历史记录??? 我的控制器上有这个: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
[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>();
}
}
这与下面的问题类似:
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]