3

I am trying to learn how to use LinqServerModeDataSource DevExpress control. The promise is that control will accept queryable (query), dynamically modify it, and pull from the database server only data that needs to be displayed. That should bring faster performance when dealing with large data sets.

I am using Entity Framework 4.1. All my objects are created with ADO.NET POCO Entity Generator (ObjectContext)

What I have done so far:

I dropped on page ASPxGridView and LinqServerModeDataSource control, and did all necessary wiring...

In LinqServerModeDataSource1_Selecting method I am having something like this:

e.KeyExpression = "CarwashId";
e.QueryableSource = Carwashes.GetCarWashes().AsQueryable();

My GetCarWashes method is defined like this:

public static IQueryable<Carwash> GetCarWashes()
{
    using (var ctx = new LMEntities(PBase.EntityFrameworkConnectionString))
    {
        var query = from c in ctx.Carwashes
                    join d in ctx.Stations on c.StationId equals d.StationId
                    orderby d.Number
                    select c;
        return query;
     }
}

The problem is I always get empty grid, with no result. By what I learned so far about EF, it makes sense because query is actually not ever evaluated. If I change return statement in above method to return query.ToList() then, of course, I get my records shown in the grid, but isn't that defeating the purpose of LinqServerModeDataSource Control?

Question:

I know that this is connected to the issue that data context is being disposed. So, If I inside LinqServerModeDataSource1_Selecting place code like this:

var ctx = new LMEntities();
e.QueryableSource = from c in ctx.Carwashes
                    join d in ctx.Stations on c.StationId equals d.StationId
                    orderby d.Number
                    select c;

everything works fine and records are displayed.

But if I use dispose context like this (notice using statement):

using (var ctx = new LMEntities());
{
    e.QueryableSource = from c in ctx.Carwashes
                        join d in ctx.Stations on c.StationId equals d.StationId
                        orderby d.Number
                        select c;
}

In this case I get no data rows.
So, What is happening here? I was thought always to use using statement to ensure proper object disposal. Also, in my application, it is required to keep data layer in separate project. If I have to have open data context, then how do I maintain this separation?

Thanks

bobetko
  • 5,019
  • 14
  • 58
  • 85
  • Seems that you should keep the context alive for the duration of the page and dispose is when the page is [unloaded](http://stackoverflow.com/a/2117924/861716). – Gert Arnold Oct 16 '12 at 09:20

0 Answers0