During insert: You cannot modify a deleted entity

Feb 16, 2010 at 8:47 PM

I am not sure why I get this error exactly: "You cannot modify a deleted entity" in PropertyChanging.

I have a table with a child table. When the user inserts a few rows into the child and deletes one I call SetAsDeleteOnSubmit() and it changes the state to CancelNew (for the row they no longer want to insert). Both the table and child are new records.

There seems to be an issue deleting an item that was New. Just before SubmitChanges() is called I check the state of the deleted row and it is Deleted, not CancelNew anymore, not sure that matters.

What might I be doing wrong here?

Dan

Coordinator
Feb 17, 2010 at 8:45 AM

Hi Dan,

It sounds like you don't have the latest code.

I Modified the below code a while back so that CancelNew remains CancelNew

Can you check the SetAsDeleteOnSubmit function to see if it's correct (as below)?

Cheers

Matt.

        public void SetAsDeleteOnSubmit()
        {
            if (this.LINQEntityState == EntityState.Detached)
                throw new ApplicationException("You cannot modify the Entity State from 'Detached' to 'Delete' ");

            if (this.LINQEntityState == EntityState.NotTracked)
                throw new ApplicationException("You cannot change the Entity State when the Entity is not change tracked");

            if (this.LINQEntityState == EntityState.New)

                this.LINQEntityState = EntityState.CancelNew;

            else if(this.LINQEntityState != EntityState.CancelNew)


                this.LINQEntityState = EntityState.Deleted;

        }

Feb 17, 2010 at 7:10 PM

That was it thanks!