views:

36

answers:

2

i am using ASP.NET MVC and EF and in my controller i am using something like this:

public ActionResult Index()
        {
            using (MyEntities db = new MyEntities())
            {
                var _info = db.INFORMATION;
                return View(_info);
            }

        }

and it throws me the below error. (if i use using statements )

error:

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

and in my VIEW i am calling like this:

   <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC_EF_Model.INFORMATION>" %>

 <% foreach (var item in Model) { %>

        <tr>...........
        ..............
A: 

Are you trying to get some child information from the object? This could cause the problem as you no longer have a connection to the db.

What version of MVC and EF are you using?

If its 3.5 then there's no or limited lazy loading.

If its 4 you may need to set lazy loading to true.

Simon G
+3  A: 

You need to enumerate your collection before to pass it to your view. Something like this:

 return View(_info.ToArray());

Indeed you are using an using so your datacontext does not exist anymore when performing the request that loads the infos entities. So you need to indicates it to perform this request before it is disposed.

Gregoire