我有一个GridView
,其DataSource
设置为BindingSource
,而Datasource
属性设置为自定义IEnumerable <SomeObject>
变量。此自定义对象来自实体框架的数据上下文。当我尝试修改GridView
中的值时,我收到错误&#39; The ObjectContext instance has been disposed and can no longer be used for operations that require a connection error
&#39;。这是可以理解的,因为我在处理数据上下文时进行了这些修改,但有一种方法可以将这些更改存储在IEnumerable变量中并禁用它的绑定&#39;那个处理过的数据背景?
编辑:为了简化,如果我声明一个新的List并将其设置为上面提到的BindingSource的DataSource,那么当从GridView更新行时,我可以看到传播到这个List对象的更改。但是当这个对象是实体框架的数据上下文中的几个查询的结果时,如何做同样的事情,这些查询似乎是附加了&#39;对结果物体的某种残余。
以下是相关代码:
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;
}
}
答案 0 :(得分:0)
我使用以下方法禁用了实体框架的Lazy Loading数据上下文:
context.Configuration.LazyLoadingEnabled = false;
这迫使数据上下文一次检索所有值。但是,我想听听关于为什么AsNoTracking()
方法不起作用的解释。