Difference in forms of exclusion with EF

4

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

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

And using EntityState.Deleted;

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

I know that with both forms you can exclude. Do they have the same function?

Can I use any one at any time?

Are there any specific cases to use?

    
asked by anonymous 24.05.2017 / 05:08

1 answer

3
  

I know that with both forms you can exclude. Do they have the same function?

Yes.

  

Can I use any one at any time?

Yes.

  

Are there any specific cases to use?

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

For checking too many things, Remove() is more recommended to use. For example, if the operation involves exclusion cascade and the key may be null in the entity that will not be deleted, using EntityState.Deleted may leave the orphaned record because there is no complete verification of the aggregation, but only the record marked for deletion .

    
24.05.2017 / 05:16