4

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

3 Answers 3

2

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().

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

Yes, the beforehand deleted entities will be included in resultEntities. Beacause, the RemoveRange didn't get reflected to the database. And, then when you are getting the results for resultEntites, you are querying the database, so it would result in the deleted values as well.

If you don't want the removed values to appear then you should call the Dbcontext.Savechanges() after RemoveRange.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.