我正在使用EF 6。
为什么ObjectDisposedException
处有Context.Entry().Reload()
?
首先,我调用Context.SaveChanges (true)
,然后立即调用Context.Entry (outDoc) .Reload ()
尝试从数据库重新加载信息。
有时此时可能会出现ObjectDisposedException
。对Dispose ()
方法的调用不在它们之间。
请告诉我,可能是什么问题?
例外:
System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
at System.Data.Entity.Core.Objects.ObjectContext.ReleaseConnection()
at System.Data.Entity.Core.Objects.ObjectContext.RefreshEntities(RefreshMode refreshMode, IEnumerable collection)
at System.Data.Entity.Core.Objects.ObjectContext.Refresh(RefreshMode refreshMode, Object entity)
at System.Data.Entity.Internal.InternalEntityEntry.Reload()
at System.Data.Entity.Infrastructure.DbEntityEntry`1.Reload()
at WCF_SupplierPortal.Controller.DBController._InsertOutgoingDocument(OutgoingDocument data)
代码:
namespace Controller
{
public class DBController
{
#region .ctor()
public DBController()
{
Context = new LPTransitContext();
}
internal DBController(ILPTransitContext context)
{
Context = context;
}
/// <inheritdoc />
~DBController()
{
Context?.Dispose();
}
#endregion
private static readonly object OutDocLocker = false;
internal readonly ILPTransitContext Context;
internal LPTransitModel.OutgoingDocument _InsertOutgoingDocument(InModel.OutgoingDocument data)
{
lock (OutDocLocker)
using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {IsolationLevel = System.Transactions.IsolationLevel.Serializable}))
{
LPTransitModel.OutgoingDocument outDoc = new LPTransitModel.OutgoingDocument
{
OriginalDocumentNumber = data.OriginalDocumentNumber,
EdoInvoiceFileName = data.EdoInvoiceFileName,
EdoAuxiliaryFileName = data.EdoAuxiliaryFileName,
...
};
LPTransitModel.OutgoingDocument dbOutgoingDocument;
try
{
dbOutgoingDocument = (from outDocument in Context.OutgoingDocument
where
...
select outDocument).FirstOrDefault();
if (dbOutgoingDocument == null && ((ActionTypeEnum) data.ActionType == ActionTypeEnum.NewRecord || (ActionTypeEnum) data.ActionType == ActionTypeEnum.Update))
{
//Новая запись
outDoc.IsNew = true;
Context.OutgoingDocument.Add(outDoc);
Context.SaveChanges(true);
**Context.Entry(outDoc).Reload(); //PROBLEM HERE**
dbOutgoingDocument = outDoc;
}
else
{
//Удаление или обновление
if (dbOutgoingDocument == null) throw new ObjectNotFoundException($"No records to Delete");
outDoc.id = dbOutgoingDocument.id;
outDoc.IsNew = dbOutgoingDocument.IsNew;
outDoc.IsUpdate = dbOutgoingDocument.IsUpdate;
outDoc.IsDelete = dbOutgoingDocument.IsDelete;
outDoc.TransactionNum = dbOutgoingDocument.TransactionNum;
switch ((ActionTypeEnum) data.ActionType)
{
case ActionTypeEnum.NewRecord:
case ActionTypeEnum.Update:
outDoc.IsUpdate = true;
outDoc.IsDelete = false;
Context.Entry(dbOutgoingDocument).CurrentValues.SetValues(outDoc);
Context.SaveChanges(true);
**Context.Entry(dbOutgoingDocument).Reload(); //PROBLEM HERE**
break;
case ActionTypeEnum.Delete:
dbOutgoingDocument.IsNew = false;
dbOutgoingDocument.IsUpdate = false;
dbOutgoingDocument.IsDelete = true;
Context.SaveChanges(true);
**Context.Entry(dbOutgoingDocument).Reload(); //PROBLEM HERE**
break;
case ActionTypeEnum.DoNothing:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
catch (Exception ex)
{
HandleException(ex);
throw;
}
try
{
scope.Complete();
}
catch (Exception ex)
{
HandleException(ex);
throw;
}
return dbOutgoingDocument;
}
}
}
}