Can i, for example, disable the
autoDetectChanges when i initialize the context, and just call
DetectChanges() before calling
Will the context recognize the inserted/changed/deleted entities?
var _dbContext = new ProjectContext(); _dbContext.Configuration.AutoDetectChangesEnabled = false; // add/edit/delete entities _dbContext.ChangeTracker.DetectChanges(); _dbContext.SaveChanges();
Should this approach work? or it may create hidden bugs?
No call to EF code will leave the context in a state where DetectChanges needs to be called if it didnâ€™t need to be called before.
Regarding Add and Delete these are "EF code" methods because you either call
Delete or you set the state of
Deleted. So, if you would just loop through a bunch of entites and add or delete them then you don't need to call
DetectChanges at all.
Regarding Edit it is, I believe, a bit more subtle. When you update entities by using either...
...or by using the property API of
context.Entry(entity).Property(e => e.SomeProperty).CurrentValue = someValue;
...then you don't need
DetectChanges (even not before
SaveChanges) either because these are again calls into "EF code".
If you just change property values of an entity like...
entity.SomeProperty = someValue;
...then the second rule in the same blog post linked above applies:
Any time that non-EF code changes any property value of an entity or complex object then DetectChanges may need to be called.
And I think you need in fact only one single call to
SaveChanges if you just loop through some entities, load them into or attach them to the context and change some (scalar and complex) property values.
If you do more complex stuff (maybe relationship changes? or something else?) your approach might not be safe anymore because
AutoDetectChanges would not be implemented in the way it is and called in many EF methods if it would be only necessary once right before
it is mentioned in the same blog post again that
If the code makes change changes to the properties of the entities instead of just calling Add or Attach, then, by Rule 2, DetectChanges will need to be called, at least as part of SaveChanges and possibly also before then.
(Highlighting from me)
Unfortunately I don't know an example of code that would show when calling
DetectChanges at earlier stages than right before
SaveChanges is necessary. But because of point 1 above I am sure such examples exist.