I ran into a problem when switching from the Local Development Server to the Local IIS Server (ASP.NET MVC4) using the following Controller-Method:
public JsonResult GetJsonData(string Context, string Language)
{
using (KeyValueDBContext db = new KeyValueDBContext())
{
var entries = from u in db.KeyValues
where ((u.Context == Context) && (u.Language == Language))
select u;
return Json(entries, JsonRequestBehavior.AllowGet);
}
}
Using the local server, I received data when calling the method from Javascript without a problem. The method retrieves a collection of basically Key-Value pairs from a database repository and sends them to the client). After switching to IIS I got an Exception telling me that the dbcontext had already been disposed of (although the using clause ends after the return-statement). (NB: Visual Studio also was not able to find JSONSerializer.cs for some reason, but only when the error occurred). Switching to the following version solved the problem completely:
public JsonResult GetJsonData(string Context, string Language)
{
KeyValueDBContext db = new KeyValueDBContext();
var entries = from u in db.KeyValues
where ((u.Context == Context) && (u.Language == Language))
select u;
return Json(entries, JsonRequestBehavior.AllowGet);
}
In both cases, this was the using-block:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using beepov4.Models; // project-models
My question: Is this an acceptable way to use dbcontext for the purpose of JSON-calls (and dropping the using-clause) or is there a particular downside or hidden problem I should be aware of?