Data Conflict in LINQ

后端 未结 7 2064
不知归路
不知归路 2020-12-13 02:33

When making changes using SubmitChanges(), LINQ sometimes dies with a ChangeConflictException exception with the error message Row not found

7条回答
  •  借酒劲吻你
    2020-12-13 03:21

    These (which you could add in a partial class to your datacontext might help you understand how this works:

    public void SubmitKeepChanges()
    {
        try
        {
            this.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        catch (ChangeConflictException e)
        {
            foreach (ObjectChangeConflict occ in this.ChangeConflicts)
            {
                //Keep current values that have changed, 
    //updates other values with database values
    
                occ.Resolve(RefreshMode.KeepChanges);
            }
        }
    }
    
    public void SubmitOverwrite()
    {
        try
        {
            this.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        catch (ChangeConflictException e)
        {
            foreach (ObjectChangeConflict occ in this.ChangeConflicts)
            {
                // All database values overwrite current values with 
    //values from database
    
                occ.Resolve(RefreshMode.OverwriteCurrentValues);
            }
        }
    }
    
    public void SubmitKeepCurrent()
    {
        try
        {
            this.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        catch (ChangeConflictException e)
        {
            foreach (ObjectChangeConflict occ in this.ChangeConflicts)
            {
                //Swap the original values with the values retrieved from the database. No current value is modified
                occ.Resolve(RefreshMode.KeepCurrentValues);
            }
        }
    }
    

提交回复
热议问题