0

I am using a dropdown in a view which is as:

@Html.DropDownListFor(model => model.BankForProfileMIS, new SelectList(@FarmerProfiler.Common.CommonData.GetBanks()), "Select", new { id = "ddlBankForProfileMIS", style = "width:155px" })

GetBanks() retreives the list of banks from database which is as below:

static DataAccessLayer.DataAccess dataAccess = new DataAccessLayer.DataAccess();
public static List<string> GetBanks()
        {
            var Banklist = (from res in dataAccess.BankNames where res.StateId.ID == 147 select res.BankName).Distinct().OrderBy(m => m).ToList();
            Banklist.Add("Other");
            return Banklist;   

        }

Same code works fine in local and UAT server but in production it has started showing the following exception message recently:

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

It used to work fine in production until yesterday. Can anyone please help in solve this problem?

Saket Kumar
  • 4,363
  • 4
  • 32
  • 55
  • Please show the code querying the Database. What does dataAccess.BankNames return? – Yakimych Feb 05 '14 at 09:28
  • @Yakimych Database is queried using Linq which i have already shown in the code. It returns distict bank names from the table. – Saket Kumar Feb 05 '14 at 11:34
  • I can see `DataAccessLayer.DataAccess` in your code, which I assume is your own class. What I don't see is the `context` object and how it is being used. "*It returns distict bank names from the table*". `BankList` is the list of distinct bank names, my question was about `dataAccess.BankNames`. – Yakimych Feb 05 '14 at 11:43
  • So `DataAccess` inherits `DbContext`, which in turn implements `IDisposable` and should be disposed. Do I understand correctly that your `dataAccess` object is long-lived and you never dispose it? If so, you should change the implementation so that your `dataAccess` object is created and disposed on every HTTP request. See http://stackoverflow.com/questions/21524426/mvc-ef-context-instances/21525157#21525157 – Yakimych Feb 05 '14 at 13:06
  • Okay its like this. `public class DataAccess : DbContext { public DataAccess() : base("DataAccess") { } public DbSet BankNames { get; set;} }` I am creating static object of DataAccess class which is implementing DbContext. – Saket Kumar Feb 05 '14 at 13:07
  • @Yakimych What happens when i dont dispose in every http request? Earlier it used to work fine in server and suddenly it started showing this exception. Any specific reason for this? – Saket Kumar Feb 05 '14 at 13:10
  • 1
    See http://stackoverflow.com/questions/3028628/application-lifetime-in-asp-net. My guess is that when you run this in production and the application pool times out, your `DbContext` dies with it. As soon as the next request comes in, you get the `ObjectDisposedException`. You probably never experienced this in your test environment since the server never timed out. Generally, your design seems to be unsuitable for a web application, and I recomment you investigate how web applications work (HTTP request-response, application pool lifetime, etc) before remodelling your code. – Yakimych Feb 05 '14 at 13:52

0 Answers0