Hi I am fairly new to entity framework and was wondering what is better when trying to remove from the context.
Is it better to use .RemoveRange passing through the collection or do my own loop and call the .Remove method?
It may be that they end up doing the same thing anyway, but I am interested to know which is preferred?
RemoveRange was added mostly as a convenience, but there can be a performance benefit to using it. This is from the source code on CodePlex:
Note that if DbContextConfiguration.AutoDetectChangesEnabled is set to true (which is the default), then DetectChanges will be called once before delete any entities and will not be called again. This means that in some situations RemoveRange may perform significantly better than calling Remove multiple times would do.
DetectChanges can have pretty big performance issues if you have a lot of entities tracked in your context. Doing this many times in a loop can be bad for performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With