c# - How to forbid lazy-loading of child object due to serialization? -


i'm using entity framework pull data expose in web api service. our model has productdetails object, has product object, has category object. so, in context called action, ask include product (but not category):

results = context.productdetails.include("product"); 

so, in service result, e.g. (xml shortened typing):

<productdetails>     ...     <product>         ...         <category>             <name>example category name</>             ...         </>         <categoryid>1</>     </>     <productid>17</>  </> 

i presume serialization causes category "lazy-loaded" though don't want to. how avoid getting more asked for? i'd get:

<productdetails>     ...     <product>         ...         <category i:nil="true"/>         <categoryid>1</>     </>     <productid>17</>  </> 

how query (or tell serializer) retain categoryid not retrieve full category?

edit, reference, here's product's model:

[table("product", schema = "myschema")] public class productsqlmodel {     private customtypeconverter _converter = new customtypeconverter();      [key]     [column("code")]     [databasegenerated(databasegeneratedoption.none)]     public virtual int? id { get; set; }      [column("category_code")]     public virtual int? categoryid { get; set; }      [foreignkey("category_id")]     public virtual categorysqlmodel category { get; set; }      ... } 

edit: i'm getting inconsistent behavior--the first load after iis reset, data want it. if reload page, category populated. clue?

i suspect lazy loading cause since serializers access each property of object in question. perhaps turn off lazy loading entire context , use eager loading? following shows how turn off lazy loading context:

public class somecontext : dbcontext  {      public somecontext()      {          this.configuration.lazyloadingenabled = false;      }  } 

you should end serialized object containing categoryid, not category object itself.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -