![](/img/trans.png)
[英]How to avoid “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection” error?
[英]Saving log information Update operation get “objectcontext instance has been disposed and can no longer be used for operations”
我正在嘗試僅將修改后的字段保存在日志表上(為此,我將兩個模型分別比較新舊模型),但在與其他表有關系的字段上出現此異常“ objectcontext實例已被放置並且不能再用於操作” 。 我該如何跳過?或者您能給我任何建議嗎?
foreach (PropertyInfo cur in newmodel.GetType().GetProperties()){
try
{
var original = model.GetType().GetProperty(cur.Name).GetValue(model, null);
var current = newmodel.GetType().GetProperty(cur.Name).GetValue(newmodel, null);
if (original != null && !original.Equals(current))
{
parameters += cur.Name + " : " + original + "Change to" + current + "<br>";
}
}
您需要在連接打開時執行此操作,因此在初始化DBContext時,需要使其保持打開狀態並using
statemtns進行操作:
using (var context = new DBContext())
{
// Perform data access using the context
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.