This is a migrated thread and some comments may be shown as answers.

Web Api eager loading related objects

2 Answers 97 Views
Web Services
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daniel
Top achievements
Rank 1
Daniel asked on 30 Sep 2013, 03:53 PM
I have a web api action as follows:

[HttpGet]
public IQueryable<Application> TestApplicationList()
{
    var entityModel = new EntityModel(
        "Data Source=.;Initial Catalog=Motion360Platform;Trusted_Connection=True",
        new BackendConfiguration
        {
            Backend = "mssql"
        });
 
    var fs = new FetchStrategy();
    fs.MaxFetchDepth = 1;
 
 
    return entityModel.Applications.Where(w => w.Id == 37);
}
  
This returns the applicaiton with Id of 37, but is sending all navigation properties with it.  It has children, and those children have children, and those children have children, all of which is being sent to the client.  This will be the query that is run on the first page and as it is now, It looks like the entire database will be sent down with this request (the actual app will not filter to just one application).

I added the fetch strategy trying to limit the depth of related objects to be returned, but that did not work.  How do you either make it to where it does not fill any of the related objects, or to just one level of related objects.

Thanks,

2 Answers, 1 is accepted

Sort by
0
Accepted
Kristian Nikolov
Telerik team
answered on 02 Oct 2013, 03:44 PM
Hello Daniel,

Unless a fetch strategy explicitly specifies otherwise, Telerik OpenAccess ORM does not load navigation properties until they are accessed. When using Web API, the serializer will access each of the properties of the object which is being serialized. This causes the navigation properties to be loaded. In order to prevent this behavior, before returning an entity you must detach it from its context using the CreateDetachedCopy method.

The method takes as arguments the entity or entities of which it should create detached copy or copies and a fetch strategy. The fetch strategy specifies which of the reference type properties should be detached and included in the copy. If no fetch strategy is passed to the method or the passed fetch strategy is just a fresh new instance, no reference type properties will be detached and included in the copy.

The code below will return all applications with Id = 37 without any of their navigation properties. Please note that when CreateDetachedCopy recieves an argument of type IQueryable, its return type is IEnumerable, therefore I have changed the return type of the method.
[HttpGet]
public IEnumerable<Application> TestApplicationList()
{
    var entityModel = new EntityModel(
        "Data Source=.;Initial Catalog=Motion360Platform;Trusted_Connection=True",
        new BackendConfiguration
        {
            Backend = "mssql"
        });
  
    return entityModel.CreateDetachedCopy<Application>(entityModel.Applications.Where(w => w.Id == 37));
}

I hope this information will help you.

Regards,
Kristian Nikolov
Telerik
OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
0
Daniel
Top achievements
Rank 1
answered on 03 Oct 2013, 02:26 PM
Awesome, this is exactly the information I was looking for.  Thank you.
Tags
Web Services
Asked by
Daniel
Top achievements
Rank 1
Answers by
Kristian Nikolov
Telerik team
Daniel
Top achievements
Rank 1
Share this question
or