Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odata with unbound function : The related entity set or singleton cannot be found from the OData path

Tags:

c#

odata

I am looking to create an unbound function inside ProductController which return entirely different entity(Not related to Product).

[EnableQuery]
public class ProductsController : ODataController
{
        [HttpGet]
        [ODataRoute("InvokeMyUnBoundFunction(Id={id})")]
        public IHttpActionResult InvokeMyUnBoundFunction(int id)
        {
            TestUnBound testObj= new TestUnBound();
            testObj.Name = "Test" + id;
            return Ok(testObj);
        }
}

and my webApiConfig is

 ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
 builder.EntitySet<Product>("Products");
 builder.EntitySet<TestUnBound>("TestUnBounds"); //Its not related to Product.
 builder.Function("InvokeMyUnBoundFunction").Returns<TestUnBound>().Parameter<int>("Id");

But when I invoked http://localhost:port/api/odata/InvokeMyUnBoundFunction(Id=1234) I got an error message like

"The related entity set or singleton cannot be found from the OData path. The related entity set or singleton is required to serialize the payload."

Am I missed any concepts?

like image 769
Linoy Avatar asked Aug 08 '15 18:08

Linoy


2 Answers

You should use

ReturnsFromEntitySet<TestUnBound> 

instread of

Returns<TestUnBound>
like image 56
Fan Ouyang Avatar answered Nov 13 '22 10:11

Fan Ouyang


In OData v4, change the function declaration to:

builder.Function("InvokeMyUnBoundFunction").ReturnsCollectionFromEntitySet<TestUnBound>().Parameter<int>("Id");

instead of ReturnsFromEntitySet<TestUnBound> as suggested by answer.

like image 40
Eric Wood Avatar answered Nov 13 '22 12:11

Eric Wood