How to soft-delete an entity object?

Y

Yash Ganthe

Hi,

I only have the ID of the object to be deleted. I want to issue an
UPDATE stmt on the DB so that the is_deleted field in the object gets
marked as true, thereby soft-deleting the object. I could do :

c = context.Customers.Where(Customer => Customer.id==123) ;
context.Attach(c) ;
c.is_deleted = true ;
context.SaveChanges() ;

But this would mean firing a SELECT stmt and then an UPDATE that
updates ALL the columns.
What I would like to do is fire an UPDATE on a single column.

I use .NET 3.5 sp1.

Regards,
Yash
 
P

Patrice

Hello,
c = context.Customers.Where(Customer => Customer.id==123) ;
context.Attach(c) ;
c.is_deleted = true ;
context.SaveChanges() ;

But this would mean firing a SELECT stmt and then an UPDATE that
updates ALL the columns.

How have you seen that all columns are updated ? It should update only the
column that actually changed. Not sure why you issue an attach statement ?
IMO :

c = context.Customers.Where(Customer => Customer.id==123) ;
c.is_deleted = true ;
context.SaveChanges() ;

Should be enough (select all columns but update only is_deleted). Use the
profiler tool for your DB to see the actual SQL statements used...

Or what you see could be the optimistic handling (i.e all columns in the
where clause, not in the update clause) in which case you want to use a
rowversion column...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top