0

我尝试用相同的数据更新两次,但它抛出异常

    public void UpdateOnSubmit<T>(T data) where T : class
    {
        lock (_lockObj)
        {
            using (DataModel dx = new DataModel(this._adapter.ConnectionString))
            {
                dx.GetTable<T>().Attach(data);
                dx.Refresh(RefreshMode.KeepCurrentValues, data);
                dx.SubmitChanges();
            }
        }
    }

例外是

An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.

第一次更新是成功的,但不是第二次。提前致谢

问候,

布赖恩

4

1 回答 1

0

好吧,好像我又找到了解决方案。我只是将 DeferredLoadingEnabled 设置为 false ,它就像一个魅力。

    public void UpdateOnSubmit<T>(T data) where T : class
    {
        lock (_lockObj)
        {
            using (DataModel dx = new DataModel(this._adapter.ConnectionString))
            {
                dx.DeferredLoadingEnabled = false;
                dx.GetTable<T>().Attach(data);
                dx.Refresh(RefreshMode.KeepCurrentValues, data);
                dx.SubmitChanges();
            }
        }
    }
于 2012-09-14T04:07:11.047 回答