Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Remove vs State.Deleted

Plain and simple, is there any difference between

DbSet().Remove(x)

to

context.Entry(x).State = State.Deleted?

Thanks


1 Answers

Not that I can tell, the MSDN article for Remove says

Marks the given entity as Deleted such that it will be deleted from the database when SaveChanges is called. Note that the entity must exist in the context in some other state before this method is called.

.Remove is likely just the preferred way to remove items.

EDIT:

Also, the MSDN article for the EntityState says this for deleted.

The entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called.

Which just further solidifies that they are effectively the same thing.

like image 160
Justin Packwood Avatar answered Jun 16 '26 07:06

Justin Packwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!