web services - Calling Related Object of the Entity from ASP.Net Web Api OData Controller -


i'm writing asp.net web api odata northwind database. here method in controller.

[enablequery] public iqueryable<order> getordersfromcustomer([fromodatauri] string key) {    // return _context.orders.where(o => o.customerid == key);     return _context.customers.where(c => c.customerid.equals(key)).selectmany(c => c.orders); } 

this configuration code in webapiconfig.cs.

     public static class webapiconfig      {          public static void register(httpconfiguration config)          {         config.routes.mapodataroute("northwind", "odata", getimplicitedm());          config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{id}",             defaults: new { id = routeparameter.optional }         );        }        private static iedmmodel getimplicitedm()       {         odatamodelbuilder builder = new odataconventionmodelbuilder();         builder.entityset<customer>("customers");         builder.entityset<order>("orders");         return builder.getedmmodel(); // magic happens here       } 

this order.cs

  public class order   {     public int orderid { get; set; }     public string customerid { get; set; }     public int? employeeid { get; set; }     public datetime? orderdate { get; set; }     public datetime? requireddate { get; set; }     public datetime? shippeddate { get; set; }     public icollection<orderdetail> orderdetails { get; set; }   } 

this customer.cs.

    public class customer     {      public string customerid { get; set; }      public string companyname { get; set; }      public string contactname { get; set; }      public string phone { get; set; }      public string address { get; set; }      public string city { get; set; }      public string postalcode { get; set; }      public string country { get; set; }      public icollection<order> orders { get; set; }     } 

this request url.

http://localhost:21288/odata/customers('alfki')/orders

every time call request, exception ( status code 500). write , check sample code. code works , code doesn't work.

"odata.error":{     "code":"","message":{       "lang":"en-us","value":"an error has occurred."     },"innererror":{       "message":"the 'objectcontent`1' type failed serialize response body content type 'application/json; odata=minimalmetadata; streaming=true; charset=utf-8'.","type":"system.invalidoperationexception","stacktrace":"","internalexception":{         "message":"null collections cannot serialized.","type":"system.runtime.serialization.serializationexception" 

i don't know wrong. plz me.

i think

_context.customers.where(c => c.customerid.equals(key)).selectmany(c => c.orders); 

can not convert iqueryable<order>, return null, , exception, try:

_context.customers.where(c => c.customerid.equals(key)).selectmany(c => c.orders).asqueryable(); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -