Search code examples
c#entity-frameworkdatacontext

Cut 'binding' with Entity Data context after object retrieval


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;

                    }

                }

Solution

  • 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.