代码之家  ›  专栏  ›  技术社区  ›  jp2code

对于DataContext,GetChangeSet在提交更改后停止工作

  •  1
  • jp2code  · 技术社区  · 8 年前

    在我们的项目中,我们有多个 System.Data.Linq.DataContext 不同Windows窗体的对象。

    当表单上的数据绑定控件发生更改(如名称)时,我们通过检查 GetChangeSet 方法

    private void ContentChanged(object sender, EventArgs e)
    {
        var changes = GetChangeSet();
        int numChanges = changes.Deletes.Count + changes.Inserts.Count + changes.Updates.Count;
        btnSubmit.Enabled = (numChanges > 0);
    }
    

    单击保存按钮时,我们调用 SubmitChanges 在林克 数据上下文 然后拨打 ClearCache extension 在 数据上下文 :

    bool IFormPanelAccessor.CommitChanges<T>(T record)
    {
        _dc.SubmitChanges();
        _dc.ClearCache();
    }
    

    ClearCache 在静态扩展类中定义如下:

    public static void ClearCache(this DataContext dc)
    {
        dc.GetType().InvokeMember("ClearCache", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, dc, null);
    }
    

    保存后 数据上下文 不再检测到 GetChangeSet 。

    有一个 触发 在 插入 或 使现代化 .这会不会打破 数据上下文 的检测更改的能力?

    我如何获得 GetChangeSet 在调用后检测数据绑定控件上的更改 提交更改 ?

    1 回复  |  直到 8 年前
        1
  •  0
  •   jp2code    8 年前

    类似 jdweng 在最初的问题中,我找到了重新加载数据的方法。

    private void CommitChanges()
    {
        // mCustomer is a record from the CustomerRecords Linq DataTable
        var rowId = mCustomer.RowId;
        _dc.SubmitChanges();
        _dc.ClearCache();
        // After ClearCache, it seems I need to physically pull the data again:
        mCustomer = _dc.CustomerRecords.FirstOrDefault(c => c.RowId == rowId);
        // Use the new record to update the controls on the form
        LoadData(mCustomer);
    }
    

    我猜是这样的 ClearCache 正在导致当前记录失去与数据的连接。

    事后看来似乎很简单,但我从未意识到这一点。