Skip to content

Invalid referenced object [TextBook] #911

@vladimirdonev

Description

@vladimirdonev

Simple.OData.Client.UnresolvableObjectException is throwing exception when trying to get data by a navigational property on a collection on another collection

this is the code that is causing the exception

 var client = new ODataClient("http://localhost:7027/odata");
    var response = await client.For<Student>()
        .Expand(x => x.StudentCourses
            .Select(y => y.Course.CourseTextBooks
                .Select(z => z.TextBook)))
        .Filter(x => x.StudentCourses.Any(y => y.Course.CourseTextBooks
            .Any(z => z.TextBook.Title == "BookCourse5")))
        .FindEntriesAsync();  

this is the registration

var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntityType<Student>().HasMany(x => x.StudentCourses);
modelBuilder.EntityType<StudentCourses>().HasKey(x => x.StudentId);
modelBuilder.EntityType<Course>().HasMany(x => x.CourseTextBooks);
modelBuilder.EntityType<Course>().HasMany(x => x.StudentCourses);
modelBuilder.EntityType<CourseTextBooks>().HasKey(x => x.CourseId);
modelBuilder.EntityType<TextBook>().HasMany(x => x.CourseTextBooks);
modelBuilder.EntitySet<Student>("Students");

builder.Services.AddControllers().AddOData(
    options => options.Select().Filter().OrderBy().Expand().Count().SetMaxTop(null).AddRouteComponents(
    "odata",
        modelBuilder.GetEdmModel()));

this are the domain models
`public class Student
{
public int Id { get; set; }

    public string Name { get; set; }

    public List<StudentCourses> StudentCourses { get; set; }
}`

`public class StudentCourses
{
public int StudentId { get; set; }

    public Student Student { get; set; }

    public int CourseId { get; set; }

    public Course Course { get; set; }
}`

` public class Course
{
public int Id { get; set; }

    public string Name { get; set; }

    public List<StudentCourses> StudentCourses { get; set; }

    public List<CourseTextBooks> CourseTextBooks { get; set; }
}`

`public class CourseTextBooks
{
public int BookId { get; set; }

    public TextBook TextBook { get; set; }

    public int CourseId { get; set; }

    public Course Course { get; set; }
}`

`public class TextBook
{
public int Id { get; set; }

    public string Title { get; set; }

    public List<CourseTextBooks> CourseTextBooks { get; set; }
}`

`public class StudentsController : ODataController
{
private readonly IServiceProvider _serviceProvider;

    public StudentsController(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All, MaxAnyAllExpressionDepth = 10, HandleReferenceNavigationPropertyExpandFilter = true, MaxExpansionDepth = 5)]
    public ActionResult<IQueryable<Student>> Get(ODataQueryOptions<Student> options)
    {
        var scope = _serviceProvider.CreateScope();
        var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
        return Ok(db.Students);
    }

    [EnableQuery]
    public ActionResult<Student> Get([FromRoute] int key)
    {
        var scope = _serviceProvider.CreateScope();
        var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

        return Ok();
    }
}`

OData metadata document

<?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="ODataTestApi.Models" xmlns="http://docs.oasis-open.org/odata/ns/edm"> <EntityType Name="Student"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="Edm.Int32" Nullable="false" /> <Property Name="Name" Type="Edm.String" Nullable="false" /> <NavigationProperty Name="StudentCourses" Type="Collection(ODataTestApi.Models.StudentCourses)" /> </EntityType> <EntityType Name="StudentCourses"> <Key> <PropertyRef Name="StudentId" /> </Key> <Property Name="StudentId" Type="Edm.Int32" Nullable="false" /> <Property Name="CourseId" Type="Edm.Int32" Nullable="false" /> <NavigationProperty Name="Student" Type="ODataTestApi.Models.Student" Nullable="false"> <ReferentialConstraint Property="StudentId" ReferencedProperty="Id" /> </NavigationProperty> <NavigationProperty Name="Course" Type="ODataTestApi.Models.Course" Nullable="false"> <ReferentialConstraint Property="CourseId" ReferencedProperty="Id" /> </NavigationProperty> </EntityType> <EntityType Name="Course"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="Edm.Int32" Nullable="false" /> <Property Name="Name" Type="Edm.String" Nullable="false" /> <NavigationProperty Name="CourseTextBooks" Type="Collection(ODataTestApi.Models.CourseTextBooks)" /> <NavigationProperty Name="StudentCourses" Type="Collection(ODataTestApi.Models.StudentCourses)" /> </EntityType> <EntityType Name="CourseTextBooks"> <Key> <PropertyRef Name="CourseId" /> </Key> <Property Name="CourseId" Type="Edm.Int32" Nullable="false" /> <Property Name="BookId" Type="Edm.Int32" Nullable="false" /> <NavigationProperty Name="TextBook" Type="ODataTestApi.Models.TextBook" Nullable="false" /> <NavigationProperty Name="Course" Type="ODataTestApi.Models.Course" Nullable="false"> <ReferentialConstraint Property="CourseId" ReferencedProperty="Id" /> </NavigationProperty> </EntityType> <EntityType Name="TextBook"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="Edm.Int32" Nullable="false" /> <Property Name="Title" Type="Edm.String" Nullable="false" /> <NavigationProperty Name="CourseTextBooks" Type="Collection(ODataTestApi.Models.CourseTextBooks)" /> </EntityType> </Schema> <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm"> <EntityContainer Name="Container"> <EntitySet Name="Students" EntityType="ODataTestApi.Models.Student" /> </EntityContainer> </Schema> </edmx:DataServices> </edmx:Edmx>

it is working fine in postman or with the build in httpclient from .net the api is .net 6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions