c# - ASP.Net WebAPI JSON response does not serialize entity relationships -


i'm developing asp.net mvc5 application using web api 5, odata , entity framework 6. i've created repositories , used entity framework power tools generate entity models. i've turned off lazy loading , proxy generation on dbcontext. below how i'm writing linq queries in repository classes return entities relationships;

           return repository             .query(i => i.istransaction == true)             .include(i => i.subinventory)             .include(c => c.contact)                             .orderby(q => q                 .orderby(i => i.itemfullcode))                             .select(); 

further in odataconfig file i've set;

config.formatters.jsonformatter.serializersettings.referenceloophandling             = newtonsoft.json.referenceloophandling.ignore;  config.formatters.jsonformatter.serializersettings.preservereferenceshandling             = newtonsoft.json.preservereferenceshandling.objects; 

the issue i'm struggling web api method json response not contain relationships included in linq query main entity data. idea i'm missing here?

below code of main entity.

    public partial class item : entity {     public item()     {         this.documentdatas = new list<documentdata>();         this.itemcolorsizes = new list<itemcolorsize>();         this.itemprices = new list<itemprice>();         this.items1 = new list<item>();         this.itemstocks = new list<itemstock>();         this.seasonalsaledetails = new list<seasonalsaledetail>();     }      public string itemfullcode { get; set; }     public string itemcode { get; set; }     public string itemname { get; set; }     public string itemshortcode { get; set; }     public string levelitemfullcode { get; set; }     public string suppliercode { get; set; }     public string subinvcode { get; set; }     public nullable<decimal> purchaseprice { get; set; }     public nullable<decimal> saleprice { get; set; }     public nullable<system.datetime> arrivaldate { get; set; }     public string refcode { get; set; }     public nullable<decimal> tcolumn { get; set; }     public nullable<bool> tcolumnbyamt { get; set; }     public nullable<bool> isgiftitem { get; set; }     public nullable<bool> istransaction { get; set; }     public nullable<bool> isactive { get; set; }     public string createdby { get; set; }     public nullable<system.datetime> createddate { get; set; }     public string modifiedby { get; set; }     public nullable<system.datetime> modifieddate { get; set; }     public contact contact { get; set; }     public icollection<documentdata> documentdatas { get; set; }     public icollection<itemcolorsize> itemcolorsizes { get; set; }     public icollection<itemprice> itemprices { get; set; }     public icollection<item> items1 { get; set; }     public item item1 { get; set; }     public subinventory subinventory { get; set; }     public icollection<itemstock> itemstocks { get; set; }     public icollection<seasonalsaledetail> seasonalsaledetails { get; set; } } 

if you're using odata query can use $expand property reference included in response. e.g.

get http://localhost/odata/products(1)?$expand=products/supplier

will result in loading supplier details rather returning id of supplier.

{   "odata.metadata":"http://localhost/odata/$metadata#categories/@element",   "products":[     {       "supplier":{"key":"ctso","name":"contoso, ltd."},       "id":1,"name":"hat","price":"15.00","categoryid":1,"supplierid":"ctso"     },     {       "supplier":{"key":"ctso","name":"contoso, ltd."},       "id":2,"name":"scarf","price":"12.00","categoryid":1,"supplierid":"ctso"     },{       "supplier":{         "key":"fbrk","name":"fabrikam, inc."       },"id":3,"name":"socks","price":"5.00","categoryid":1,"supplierid":"fbrk"     }   ],"id":1,"name":"apparel" } 

see article details.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -