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


I am using this code in my asp.net MVC boilerplate application

 public ActionResult ShowQuote(int id)
        {
            var qtm = new QuoteTableMain();
            var AddtionalDetails = new AddtionalDetail();
           
                using (var context = new bwavenueEntities())
                {
                    //_unitOfWorkManager.Current.EnableFilter(AbpDataFilters.MustHaveTenant);
                   
                    if (context.QuoteTableMains.Where(a => a.Id == id).FirstOrDefault().MainTenantId == _userManager.AbpSession.TenantId)
                    {
                        qtm = context.QuoteTableMains.Include(a => a.Products).Include(a => a.AddtionalDetails).Where(a => a.Id == id).FirstOrDefault();
                    
                    }
                    else
                    {

                    return RedirectToAction("E404","Error",new { area=""});
                    }
                    //AddtionalDetails= context.AddtionalDetails.Where(a=>a.ProductId==qtm.
                }

                return View("GetSavedQuote", qtm);
            
           
        }

After fetching data, in the view, I am trying to Render details of .Products.NetworkImage , which is throwing error

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

here is the code from view and line in which I am getting the error

@foreach (var product in Model.Products.Where(a => a.Supplier == "Comcast"))
            {
                <tr data-id="@product.Id">
                    <td>@product.Supplier </td>
                    <td>@Html.Raw(product.Description)</td>
 <!--Getting Error here-->
                    <td><img src="@(product.NetworkNameAndImage != null ? product.NetworkNameAndImage.ImageUrl :"" )" alt="@product.Network" /></td> 
                    <td>
                      
                        @product.BandWidth
                    </td>
                    <td>@product.ContractTerm</td>
                    <td> $@string.Format("{0:0.00}", product.MonthlyReCurringCharge)/mo</td>
                    <td>
                        <span class="plusButton" data-show="true">+</span>
                        <span class="clear"></span>
                        <div class="hide MoreDetails">

                            <span class="productdetails">

                                Installation Charge- $@product.InstallationCharge   <br />
                                Loop Charge- $@product.LoopCharge <br />
                                Network -  @product.Network

                            </span>
                        </div>
                    </td>
                    <td><a href="javascript:void(0)" class="btn btn-default OrderNow" data-id="@product.Id" target="_blank">Order</a></td>
                </tr>
            }

I have already used .Include(a=>a.Products) in C# code, so why i am getting this issue, and how can i resolve it?


Asked by:- bhanu
1
: 94 At:- 9/26/2017 1:13:00 PM
asp.net C# System.ObjectDisposedException Entity-framework






1 Answers
profileImage Answered by:- Vinnu

As you can understand from the error, your Context has been disposed(The ObjectContext instance has been disposed), and code is trying to get new data here (product.NetworkNameAndImage) using database object, which is already disposed,so your error would be resolved by disabling LazyLoading in your context, to resolve this issue, your C# code should be

using (var context = new bwavenueEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                qtm = context.QuoteTableMains.Include(a => a.Products).Include(a => a.AddtionalDetails).Where(a => a.Id == id).FirstOrDefault();    
              
            }

If the above code doesn't resolve your issue, there can be issue in relationship of your code, or may be you need to JOIN your tables accordingly and above code is would not work in this case, as .Include() should alerady resolved your error.

NOT SO Good way to make it work would be

using (var context = new bwavenueEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                qtm = context.QuoteTableMains.Include(a => a.Products).Include(a => a.AddtionalDetails).Where(a => a.Id == id).FirstOrDefault();

             
                foreach (var produc in qtm.Products)
                {
//Set your Null data here by looping it, it is not correct method, but to make it quickly work you can do this
                    produc.NetworkNameAndImage = context.NetworkNameAndImages.Where(a => a.Id == produc.NetworkImageId).FirstOrDefault();
                }
            }

NOTE: Above code is not according to good coding  standard(i would not recommend it), but you can use it to make it work for now

1
At:- 9/27/2017 8:21:37 AM
Disabling LazyLoadig worked for me, thanks 0
By : bhanu - at :- 10/9/2017 6:45:27 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly