Web Api eager loading related objects

3 posts, 1 answers
  1. Daniel
    Daniel avatar
    4 posts
    Member since:
    Mar 2013

    Posted 30 Sep 2013 Link to this post

    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. Answer
    Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 02 Oct 2013 Link to this post

    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 >>
  3. DevCraft banner
  4. Daniel
    Daniel avatar
    4 posts
    Member since:
    Mar 2013

    Posted 03 Oct 2013 Link to this post

    Awesome, this is exactly the information I was looking for.  Thank you.
Back to Top