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

Using ToDataSourceResult in Web API Controller

1 Answer 430 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vesselin Obreshkov
Top achievements
Rank 2
Vesselin Obreshkov asked on 28 Jun 2012, 04:28 PM
Hi,

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")
								   .Include("Offer.Advertiser")
								   .Include("Offer.Advertiser.Company")
								   .Include("Publisher")
								   .Include("Publisher.Company")
								   .Include("Publisher.Employee")
								   .Include("Publisher.Employee.Profile")
								   .Include("Publisher.Profile")
								   .Include("Status");
 
		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!

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 02 Jul 2012, 12:32 PM
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.

Regards,
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!
Tags
General Discussions
Asked by
Vesselin Obreshkov
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
Share this question
or