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

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

Old forum URL: forums.lhotka.net/forums/t/5915.aspx


raz0rf1sh posted on Monday, December 01, 2008

I am running into am exception that I can't seem to figure out!  I have a parent business object, Task, with a collection of child objects of TaskStep.  I can add a TaskStep to a Task, with no problem, but whenever I try to edit or delete a TaskStep, and save the changes ... things blow up!

Row not found or changed.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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


Here is a snippet of my code ... any help would be much appreciated!

Task

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            using (var ctx = ContextManager<Gimli.Data.GimliDataContext>
                        .GetManager(Database.ApplicationConnection, false))
            {
                var data = new Gimli.Data.Task()
                {
                    TaskId = this.ReadProperty<Guid>(TaskIdProperty)
                };

                ctx.DataContext.Tasks.Attach(data);

                data.Number = this.ReadProperty<int>(NumberProperty);
                data.Status = this.ReadProperty<int>(StatusProperty);
                data.Description = this.ReadProperty<string>(DescriptionProperty);
                data.CreatedBy = new Guid(Csla.ApplicationContext.User.Identity.Name);
                data.CreatedDate = DateTime.Now;

                // update child object(s)
                DataPortal.UpdateChild(this.ReadProperty<TaskSteps>(StepsProperty), this);
                DataPortal.UpdateChild(this.ReadProperty<TaskStatusChanges>(StatusChangesProperty), this);

                ctx.DataContext.SubmitChanges();

                this.LoadProperty<Guid>(ModifiedByProperty, data.ModifiedBy);
                this.LoadProperty<DateTime>(ModifiedDateProperty, data.ModifiedDate);
            }
        }

TaskStep

        [Transactional(TransactionalTypes.TransactionScope)]
        private void Child_Update(Task parent)
        {
            using (var ctx = ContextManager<Gimli.Data.GimliDataContext>
                        .GetManager(Database.ApplicationConnection, false))
            {
                var data = new Gimli.Data.TaskStep()
                {
                    TaskStepId = this.ReadProperty<Guid>(TaskStepIdProperty)
                };

                ctx.DataContext.TaskSteps.Attach(data);

                if (this.IsSelfDirty)
                {
                    data.TaskId = parent.TaskId;
                    data.Number = ReadProperty<int>(NumberProperty);
                    data.Description = ReadProperty<string>(DescriptionProperty);
                    data.IsCompleted = ReadProperty<bool>(IsCompletedProperty);
                }
            }
        }


raz0rf1sh replied on Monday, December 01, 2008

I have discovered it has nothing to do with the children.  If I comment out the TaskStep in the update and just change a field on the Task it blows up as well ... so it's something in the Task.

raz0rf1sh replied on Monday, December 01, 2008

I loop through all the MemberChangeConflicts and the property IsModified is set to True, but what is odd is that the CurrentValue and DatabaseValues are the same.  Not sure how OriginalValue fits into the equation.

ajj3085 replied on Tuesday, December 02, 2008

The problem is you're using Attach, which tells linq the row already exists in the db.  It tries to update it, but sees that there is no such row, and so it fails.  Use InsertOnSubmit instead.

Garrison replied on Monday, March 23, 2009

Make sure you have a Timestamp column.  I had the same issue and adding a Timestamp column resolved it.

Copyright (c) Marimer LLC