OData / OData/AspNetCoreOData

$expand parameter not expanding the navigation property in EDM

Open
#83 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Metadata has a navigation property Organization shown below :

https://localhost:34345/api/Users/$metadata#Collection(Model)

<?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"> <edmx:DataServices> <Schema Namespace="Model" xmlns="http://docs.oasis-open.org/odata/ns/edm"> <EntityType Name="User"> <Key> <PropertyRef Name="UserId" /> </Key> <Property Name="UserId" Type="Edm.String" Nullable="false" /> <Property Name="FirstName" Type="Edm.String" Nullable="false" /> <Property Name="LastName" Type="Edm.String" Nullable="false" /> <NavigationProperty Name="Organization" Type="Collection(Model.UserOrganization)" /> </EntityType> <EntityType Name="UserOrganization"> <Key> <PropertyRef Name="OrganizationId" /> <PropertyRef Name="UserId" /> </Key> <Property Name="OrganizationId" Type="Edm.String" Nullable="false" /> <Property Name="UserId" Type="Edm.String" Nullable="false" /> <Property Name="OrganizationName" Type="Edm.String" /> <Property Name="IsLocked" Type="Edm.Boolean" Nullable="false" /> <Property Name="Email" Type="Edm.String" /> <Property Name="MobilePhoneNumber" Type="Edm.String" /> <Property Name="OfficePhoneNumber" Type="Edm.String" /> </EntityType> </Schema> <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm"> <EntityContainer Name="Container"> <EntitySet Name="User" EntityType="Model.User"> <NavigationPropertyBinding Path="Organization" Target="Organization" /> </EntitySet> <EntitySet Name="Organization" EntityType="Model.UserOrganization" /> </EntityContainer> </Schema> </edmx:DataServices> </edmx:Edmx>

EDM Conventional Model Builder:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<User>(DataConstant.User);

Startup.cs - IApplicationBuilder configure :
`private static void MapOdataRoutService(IApplicationBuilder appBuilder)
{

appBuilder.UseMvc(odata =>
{

odata.EnableDependencyInjection();

odata.Select().Expand().Filter().OrderBy().MaxTop(null).Count();

         odata.MapODataServiceRoute(DataConstant.UserControllerName, "api/Users", GetUserEdmModel());
  }

}`

LINQ select query to return IQueryable of User, here User and UserOrganization are DTO's manually mapping property from dbsets (users, userOrganizations)

`
return Task.FromResult((from user in users
select new User
{
FirstName = user.FirstName,
LastName = user.LastName,
UserId = user.UserId,
Organization = (from organization in userOrganizations
where organization.UserId == user.UserId
select new UserOrganization
{
UserId = organization.UserId,
OrganizationId = organization.OrganizationID,
OrganizationName = organization.OrganizationName,
IsLocked = organization.IsLocked,
Email = organization.Email,
MobilePhoneNumber = organization.MobilePhoneNumber,
OfficePhoneNumber = organization.OfficePhoneNumber,

                                                    }).ToList()
                                }));`

API URL with $Expand not showing up the organization collection in the api result :

https://localhost:34345/api/Users?$expand=Organization

{ "@odata.type": "#Model.User", "UserId": "XXX-XXXX-XXXX", "FirstName": "F", "LastName": "L" }

Other parameters like $top, $count, $filter and $select on the available fields are works good.

Used packages:

.NET 5
Microsoft.AspNetCore.OData 7.5.5
Microsoft.EntityFrameworkCore 5.0.1
Microsoft.EntityFrameworkCore.SqlServer 5.0.1
Microsoft.EntityFrameworkCore.Tools 5.0.1

Any idea on how to get the expanded result?

Thanks in advance!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Startup.cs, especially MapOdataRoutService and GetUserEdmModel, then inspect the LINQ projection that populates User.Organization. Reproduce the request at /api/Users?$expand=Organization and compare it with the EDM metadata; done means the response includes the expanded Organization collection without breaking the working query options.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.