Deleting entity with EntityState = New

Jan 15, 2009 at 2:15 PM

We create entity in some table which stored in viewstate. Next we mark this entity as deleted. Db sync throw exception on attaching non-existing entity to datacontext. Are we need to remove this entity from our table before sync db?

Jan 16, 2009 at 3:30 AM
Edited Jan 16, 2009 at 3:33 AM
Hi There,

I'm a bit confused about what you are saying, are you adding an entity to a collection in memory and then marking that same entity as delete on submit, then syncronising with the database?  

If this is the case, the database will throw a non-existing error because you are trying to delete a record from the database that never existed in the first place, and it's trying to use what ever the key is to find it.in the DB and hence can't find it.

I'll look at this when I have time, but perhaps temporarily change the LINQEntityBase so that the the SetAsDeleteOnSubmit() method code to what's below.  See if that does the job.

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.Deleted;

}

else

{

this.LINQEntityState = EntityState.NotTracked;
}

}

 

 

 

Jan 16, 2009 at 11:15 AM
Edited Jan 16, 2009 at 11:16 AM

Hi Matt,

Thanks for your code, but I didn't want to change a logic filtering deleted records in my ListView (LINQEntityState != EntityState.Deleted), so I added in LINQEntityBase an additional flag "CreatedAndDeleted". If flag is true, I'm ignore entity when it is synchronizing with db.

And sorry for my lousy English :-)

Best Regards, Bulat.