Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid referenced object [TextBook] #911

Open
vladimirdonev opened this issue Sep 4, 2023 · 0 comments
Open

Invalid referenced object [TextBook] #911

vladimirdonev opened this issue Sep 4, 2023 · 0 comments

Comments

@vladimirdonev
Copy link

vladimirdonev commented Sep 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant