System.Data.Linq.ChangeConflictException: Row not found or changed

后端 未结 19 2672
生来不讨喜
生来不讨喜 2020-12-07 22:21

I am trying to delete a selected gridview row using LINQ (No LINQDataSource).

When the selection is changed, the detailsview binding is changed also. I can add a new

相关标签:
19条回答
  • 2020-12-07 22:57

    The way i fixed it was: First I update the database, then I set the new values for the grid as

    e.Keys["ColumnOne"] ="new value"
    e.Keys["ColumnTwo"] ="new value"
    

    All this was done under the GridView_RowUpdating event.

    0 讨论(0)
  • 2020-12-07 23:00

    Also - if you're calling the selecting method for the linqdatasource and setting e.result manually, make sure you're including any foreignkey values as well.

    Nothing else worked for me but this.

    0 讨论(0)
  • 2020-12-07 23:00

    I had a similar problem and although deleting and re-adding the DBML table/class helped some users, for me it was a bit different since I'm using WCF with detached entity and a ListView on the client.

    If I used the .Attach(entity) it failed - "Row not found or changed" But when using .Attach(entity, original) it works every time

    public void DeleteTask(Task task)
        {
            TwoDooDataContext db = new TwoDooDataContext();
            db.Tasks.Attach(task,GetTaskByID(task.ID));
            db.Tasks.DeleteOnSubmit(task);
            db.SubmitChanges();
        }
    
    0 讨论(0)
  • 2020-12-07 23:01

    I just wanted to add my scenario for anybody who may have this issue.

    We use a custom T4 against our Linq to SQL dbml. We basically just modified the original get/set of string properties to automatically trim and set null.

            get { return _OfficiantNameMiddle.GetValueOrNull(); }
            set 
            {
                value = value.GetValueOrNull();
                if (_OfficiantNameMiddle != value) 
                {
                    _IsDirty = true;
                    OnOfficiantNameMiddleChanging(value);
                    SendPropertyChanging("OfficiantNameMiddle");
                    _OfficiantNameMiddle = value;
                    SendPropertyChanged("OfficiantNameMiddle");
                    OnOfficiantNameMiddleChanged();
                }
            }
    

    Legacy data in our database had some leading/trailing spaces so any concurrency check on these columns failed to result in a match (it was comparing the trimmed value against the non-trimmed database value). It was really easy to profile SQL, grab the SQL and start commenting out the items in the WHERE clause until it started returning a row during the concurrency check.

    Luckily, we have a LastUpdatedOn field in our tables that is automatically set via OnValidate(System.Data.Linq.ChangeAction).

        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (action == System.Data.Linq.ChangeAction.Insert)
            {
                CreatedBy = CurrentUserID;
                CreatedOn = DateTime.Now;
                LastUpdatedBy = CreatedBy;
                LastUpdatedOn = CreatedOn;
            }
            else if (action == System.Data.Linq.ChangeAction.Update)
            {
                LastUpdatedBy = CurrentUserID;
                LastUpdatedOn = DateTime.Now;
            }
        }
    

    In order to bypass the problem, we just set the concurrency check to Never on all columns except the Primary Key columns and LastUpdatedOn column. This worked for us.

    0 讨论(0)
  • 2020-12-07 23:01

    For me it was an enum column (mapped to a varchar) which caused the problem, so I had to pass update check to never.

    0 讨论(0)
  • 2020-12-07 23:02

    This is just a case of mismatched column definitions. Just remove the table from .dbmland re-add. Make sure to change auto generate value property = true for columns that have auto generated data such as Primary Keys or datetime columns.

    0 讨论(0)
提交回复
热议问题