Using ToDataSourceResult in Web API Controller

2 posts, 0 answers
  1. Vesselin Obreshkov
    Vesselin Obreshkov avatar
    81 posts
    Member since:
    Jan 2010

    Posted 28 Jun 2012 Link to this post


    I'm trying to use the ToDataSourceResult inside an MVC 4 Web API controller like so (wrong, I know, please help)

    public DataSourceResult Get([DataSourceRequestDataSourceRequest request)
    	DataSourceResult dataSourceResult;
    	using (MobookaEntities entities = new MobookaEntities(false))
    		IQueryable<Campaign> campaigns = entities.Campaigns.Include("Offer")
    		dataSourceResult = campaigns.ToDataSourceResult(request);
    return dataSourceResult; }

    But I'm getting the following error:

    The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

    I kind of understand why I'm getting this and if I copied the DataSourceResult object into a different object and returned that then it would work, however, I would like to know what do I need to do to have the LINQ executed and returned in my controller. Thank you!
  2. Atanas Korchev
    Atanas Korchev avatar
    8462 posts

    Posted 02 Jul 2012 Link to this post

    Hello Vesselin,

    This occurs because the DataSourceResult currently does not enumerate the data. As a result the EF context gets disposed and accessing an IQueryable after that blows up. As a solution we are now "executing" the result when creating a DataSourceResult by enumerating it. Long story short this should work with the official release.

    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET AJAX banner
Back to Top