views:

117

answers:

1

am getting this error on this code (this is an MVC project into which I am trying to integrate Entity Framework):

        List<string> consultantSchoolList = new List<string>();

        // districts managed by consultant
        IQueryable<string> consultClients = rc.consultantDistrictsRepository.districtsForConsultant(userID);

        // schools managed by consultant
        if (consultClients != null)
        {
            consultantSchoolList = (from c in rc.clientsRepository.Clients
                                    where (consultClients.Contains(c.cdsCode.Substring(0, 7)))
                                    select c.cdsCode).ToList();
        }

on the "consultantSchoolList = " line.

This is an MVC project and I am using a context object that is stored in the HttpContext.Current object. Each repository has a private variable that stores the context object, but each one should point to the same object in the HttpContext.Current Items collection. Would this be considered two different contexts even though they point to the same thing?

Stepping through the code in the debugger shows that the context objects for the two repositories, consultantDistrictsRepository and clientsRepository, do point to the same object in the HttpContext.Current object.

UPDATE Here's how I define the context objects in each repository:

    private SchedulerContext context;

    public EFConsultantDistricts()
    {
        context = ContextHelper.GetContext();
    }

and GetContext is as follows:

    public static SchedulerContext GetContext()
    {
        if (!HttpContext.Current.Items.Contains("_db_context"))
        {
            HttpContext.Current.Items.Add("_db_context", new SchedulerContext());
        }
        return (SchedulerContext)HttpContext.Current.Items["_db_context"];
    }
+1  A: 

I found the problem -- I was caching the frequently-requested clients list in a Session variable in the Clients repository:

            if (HttpContext.Current.Session["clientList"] == null)
            {
                HttpContext.Current.Session["clientList"] = from c in context.Clients
                                                            where (c.Year == fiscalYear)
                                                            select c;
            }
            return (IQueryable<Client>)HttpContext.Current.Session["clientList"];

Since the Session object persists over requests, I guess it was using a previous context object. I thought the client list would be separated from the context object, but I guess not if I'm using IQueryable.

I hate to hit the database every time for this, but I guess I have no choice ... at least for the moment until I get everything straightened out.

Cynthia
You don't have to hit the DB every time. Cache the info in a non-entity POCO instead of an entity, and transfer scalar values to entities as needed.
Craig Stuntz
Yeah, actually I don't know what I was thinking. This was not caching the contents of the client list, only the query, so it was kind of pointless. EF is still a bit of a mystery to me. Huge learning curve on this technology.
Cynthia