This is a migrated thread and some comments may be shown as answers.
linq query and serialisation of navigation properties in webapi
4 Answers 17 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Robin
Top achievements
Rank 1
Robin asked on 08 Apr 2014, 08:49 PM
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) ?

4 Answers, 1 is accepted

Sort by
0
Robin
Top achievements
Rank 1
answered on 11 Apr 2014, 06:42 PM
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. 
0
Doroteya
Telerik team
answered on 12 Apr 2014, 06:45 AM
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.

Regards,
Doroteya
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Gary
Top achievements
Rank 1
answered on 25 Aug 2015, 03:16 PM
I am experiencing issues with the FetchStrategy when applied to a CreateDetachedCopy.
01.public static DeliverablePackage GetDeliverablePackageAndContentsBySKU(String SKU)
02.{
03.    DeliverablePackage ret = null;
04.    DeliverablePackageEntity copy = null;
05. 
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();
18.         
19.        int itemCount = entity.PackageContents.Count;  // 3
20.         
21.        //
22.        // Create a DETACHED COPY of the Entity.
23.        copy = ctx.CreateDetachedCopy<DeliverablePackageEntity>(entity, strategy);
24.         
25.        int copyItemCount = copy.PackageContents.Count; // 0
26. 
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;
32.}

 

 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

 

 

0
Kristian Nikolov
Telerik team
answered on 28 Aug 2015, 08:59 AM
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.

Regards,
Kristian Nikolov
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
LINQ (LINQ specific questions)
Asked by
Robin
Top achievements
Rank 1
Answers by
Robin
Top achievements
Rank 1
Doroteya
Telerik team
Gary
Top achievements
Rank 1
Kristian Nikolov
Telerik team
Share this question
or