Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete and query, without DbContext.SaveChanges

Entity know-how question. Given the following code:

...
var entitiesToRemove = dbSet.Where (entity => entity.TimeToLive > 5);
dbSet.RemoveRange(entitiesToRemove);
var resultEntities = dbSet.Where(entity => /* some condition that will also match before deleted entities*/);
...

Question: Will the beforehand deleted entities also be included in resultEntities or not? Do I've to call DbContext.SaveChanges after dbSet.RemoveRange ?

Thx

like image 215
Moerwald Avatar asked Oct 27 '25 01:10

Moerwald


2 Answers

You need to do dbcontext.SaveChanges() before doing further processing. In dbSet.RemoveRange(entitiesToRemove), EF just have marked those entities for deletion. Which will reflect on db only after you call the SaveChanges().

like image 135
Pavvy Avatar answered Oct 28 '25 14:10

Pavvy


Yes, I think DbContext.SaveChanges should go after dbSet.RemoveRange

like image 25
Sham Avatar answered Oct 28 '25 16:10

Sham



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!