Difference in EF exclusion forms

Asked

Viewed 58 times

4

I want to know, what is the difference (if any) in the form of exclusion using the RU

db.Contexto.Remove(objeto);
db.SaveChanges();

And using the EntityState.Deleted;

Db.Entry(objeto).State = System.Data.Entity.EntityState.Deleted;
Db.SaveChanges();

I know that with both ways it is possible to delete. They have the same function?

I can use any one at any time?

Are there any specific cases for use of any?

1 answer

3


I know that with both ways it is possible to delete. They have the same function?

Yes.

I can use any one at any time?

Yes.

Are there any specific cases for use of any?

Remove() checks some more consistencies, such as the materialization of the set itself (main entity and aggregated entities) and the state of the properties, with due detection of the fields that have been changed, but there is little difference between them. When it comes to exclusion, maintaining field change status is not so important.

For checking things out, Remove() is most recommended to be used. For example, if the operation involves cascading deletion and the key can be null in the entity that will not be deleted, use EntityState.Deleted can leave the record orphaned, because there is no complete verification of the aggregation, and yes, only the record marked for deletion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.