0

I have a GridView that has its DataSource set to BindingSource, which in turn has its Datasource property set to a custom IEnumerable <SomeObject> variable. This custom object comes from Entity Framework's Data context. When I try to modify values from GridView I get an error 'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection error'. This is understandable since I do these modifications when my data context is already disposed but is there a way to just store these changes in the IEnumerable variable and disable it's 'binding' to that disposed data context?

Edit: To simplify, If I declare a new List and set it as a DataSource to the upper mentioned BindingSource, then when updating the rows from GridView, I can see the changes propagated to this List object. But how to do the same when this object is a result of several queries in Data context of Entity Framework which seems to be 'attaching' some kind of remnant to the resulting object.

Here is the relevant code:

using (AmboliCardEntities context = new AmboliCardEntities(Globals.StrEntityConnecitonString))
                {


                    var transaction = context.tblTransactions.AsNoTracking().FirstOrDefault(a => a.GUID == gTransaction);
                    if (transaction != null)
                    {
                        var ret = new TransactionData
                        {
                            Transaction = transaction,

                            ConstantAccumulations = context.tblConstantAccumulations.AsNoTracking()
                                .Where(a => a.transactionid == gTransaction && a.removed == false).AsNoTracking().ToList(),

                            Cashbacks = context.tblCashbacks.AsNoTracking()
                                .Where(a => a.transactionid == gTransaction && a.removed == false).AsNoTracking().ToList(),

                            PurchasedProducts = context.tblPurchasedProducts.AsNoTracking().Include(a => a.tblProduct)
                                .Include(a => a.tblProduct.tblProductBrand)
                                .Include(a => a.tblProduct.tblProductBrand.tblProductCategory)
                                .Include(a => a.tblVehicle)
                                .Include(a => a.tblVehicle.tblVehicleTrim)
                                .Include(a => a.tblVehicle.tblVehicleTrim.tblVehicleModel)
                                .Include(a => a.tblVehicle.tblVehicleTrim.tblVehicleModel.tblVehicleMake)
                                .Where(x => x.transactionid == gTransaction)
                                .AsNoTracking()
                                .ToList()
                        };

                        var vehicles = context.tblVehicles.AsNoTracking().Include(x => x.tblVehicleTrim)
                            .Include(x => x.tblVehicleTrim.tblVehicleModel)
                            .Include(x => x.tblVehicleTrim.tblVehicleModel.tblVehicleMake)
                            .Where(a => a.cardid == transaction.cardguid)
                            .AsNoTracking()
                            .ToList();



                        return ret;

                    }

                }
astralmaster
  • 2,344
  • 11
  • 50
  • 84
  • You need an in-memory object like List or Array. For example bind to `.ToList()` instead. https://stackoverflow.com/questions/18398356/how-to-solve-the-error-the-objectcontext-instance-has-been-disposed-and-can-no-l – Steve Greene Aug 21 '17 at 15:15
  • 1
    Use AsNoTracking() to prevent Ef from tracking the changes to entities – esnezz Aug 21 '17 at 15:17
  • Hm, I added AsNoTracking() to my query but I still get the same error. It does sound like the solution I am looking for though – astralmaster Aug 21 '17 at 15:47

1 Answers1

0

I disabled the Lazy Loading of Entity Framework's data context using:

context.Configuration.LazyLoadingEnabled = false;

And that forced the data context to retrieve all the values at once. I would, however, like to hear an explanation as to why AsNoTracking() method did not work.

astralmaster
  • 2,344
  • 11
  • 50
  • 84