linq query and serialisation of navigation properties in webapi

5 posts, 0 answers
  1. Robin
    Robin avatar
    8 posts
    Member since:
    Oct 2013

    Posted 08 Apr 2014 Link to this post

    Im having trouble with the loadwith method options. It seems simple enough, but I cant get it to work the way I want. Using the scaffolding/service generator for webapi crud basics, the Get method doesnt serialize the navigation properties, which is the way it should be. 

    However, when I make a simple linq query, it serializes the navigation properties (I only want some to load, not everything). Explicitly adding loadwith or include doesnt do a thing. 

    For example: 

        using (var db = new EntitiesModel1())
            FetchStrategy fs = new FetchStrategy();
            fs.LoadWith<ThinkDataOA.General_Organisation>(o => o.children);
            fs.MaxFetchDepth = 3;
            IQueryable<General_Organisation> emp = from general_organisation in db.General_Organisations.LoadWith(fs) select general_organisation;
            return emp.ToList<General_Organisation>();

    It returns all of the navigation properties, except for children (needed for treeview). Then as a test I try:

        using (var db = new EntitiesModel1())
            IEnumerable<General_Organisation> emp = db.General_Organisations.ToList<General_Organisation>();
            return emp; //

    This gives me the same result, even though I dont specify anything. I restarted VS to clear any caches and also checked if there were any other global fetchplans loaded. 

    Am I overlooking something obvious (all the data is correct btw) ?
  2. Robin
    Robin avatar
    8 posts
    Member since:
    Oct 2013

    Posted 11 Apr 2014 in reply to Robin Link to this post

    For those interested, due to my noobness I did not know that due to the serialization to json, all the nav. properties get traversed. And so get serialized. 

    By looking at the generated service code, it seems that using CreateDetachedCopy, in combination with a fetchstrategy, that only those properties specified in the fs will be in the serialized result. 
  3. DevCraft banner
  4. Doroteya
    Doroteya avatar
    495 posts

    Posted 12 Apr 2014 Link to this post

    Hi Robin,

    Thank you for your feedback. I am glad you managed to figure it out.

    Indeed, by default the Web API services generated by Telerik Data Access Service Wizard do not serialize the navigation properties. You can apply a fetch strategy according to your needs in a partial class that extends the implementation of the repository for the particular persistent entity. A good approach here is to override the GetAll() method that comes from the base repository class and to customize the fetch strategy in it. For example:
    public partial class MyPersistentClassRepository : OpenAccessBaseRepository<MyApplication.Model.MyPersistentClass, MyApplication.Model.EntitiesModel>
        public override System.Linq.IQueryable<MyPersistentClass> GetAll()
            this.fetchStrategy.LoadWith<MyPersistentClass>(mpc => mpc.MyNavigationProperty);
            return base.GetAll();

    I hope this helps. In case you need additional information, do not hesitate to get back to us.

    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  5. gstenstrom
    gstenstrom avatar
    27 posts
    Member since:
    Jun 2015

    Posted 25 Aug 2015 in reply to Doroteya Link to this post

    I am experiencing issues with the FetchStrategy when applied to a CreateDetachedCopy.
    01.public static DeliverablePackage GetDeliverablePackageAndContentsBySKU(String SKU)
    03.    DeliverablePackage ret = null;
    04.    DeliverablePackageEntity copy = null;
    06.    using (DAL.DarkRoomDB ctx = new DarkRoomDB())
    07.    {
    08.        //
    09.        // Create a FetchStrategy to pre-load the contents of the package
    10.        FetchStrategy strategy = new FetchStrategy();
    11.        strategy.LoadWith<DeliverablePackageEntity>(c => c.PackageContents);
    12.        strategy.MaxFetchDepth = 1;     // do not preload any of the navigation properties of the Entities returned in the FetchStrategy
    13.        ctx.FetchStrategy = strategy;
    14.        //
    15.        // get the package that matches the SKU
    16.        DataStoreRepository<DeliverablePackageEntity> repo = DataStoreRepository<DeliverablePackageEntity>.GetInstance(ctx);
    17.        DeliverablePackageEntity entity = repo.GetEntityList(c => c.PackageSku == SKU).FirstOrDefault();
    19.        int itemCount = entity.PackageContents.Count;  // 3
    21.        //
    22.        // Create a DETACHED COPY of the Entity.
    23.        copy = ctx.CreateDetachedCopy<DeliverablePackageEntity>(entity, strategy);
    25.        int copyItemCount = copy.PackageContents.Count; // 0
    27.    }
    28.    //
    29.    // Map the entity AFTER disposing the Context. Now we have no context from which to retrieve any navigation property values even if it tries to get them.
    30.    ret = EntityToDomainMapper.Map<DeliverablePackageEntity, DeliverablePackage>(copy);             
    31.    return ret;


     You will notice that on line 19 the count is 3 and after the CreateDetachedCopy the count is 0. This technique appears to work for folks so I assume that I am doing something wrong, but I can't figure out what it is.


    - G



  6. Kristian Nikolov
    Kristian Nikolov avatar
    206 posts

    Posted 28 Aug 2015 Link to this post

    Hi Gary,

    Thank you for contacting us.

    The reason of the behavior you are experiencing is that MaxFetchDepth is set to 1. Value of 1 indicates that only the targeted object will be retrieve - without any of its related object. If you want to retrieve the targeted object and its directly related objects you would need to set MaxFetchDepth = 2. Then the copied object should contain the related PackageContents.

    I hope this helps. Feel free to post at our forums again in case you have any more questions.

    Kristian Nikolov
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top
DevCraft banner