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?
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
Error occurs when you try to load referencing properties (related/relationship/navigation entities) in C# code after database connection is closed.
So, you can load referencing properties using .Include()
something like below code
var qtm = context.QuoteTableMains.Include(a => a.Products).Include(a => a.AddtionalDetails).Where(a => a.Id == id).FirstOrDefault();
In above code, we are also loading "Products" table data and "AdditionalDetails" table data, which is linked with "QuoteTableMains" database table.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly