Suppose I have the following code:
TEModule teModule = Context.TEModules.Where(module => module.EnumValue.Equals(text.ModuleName)).FirstOrDefault(); if (teModule == null) { teModule = new TEModule(); teModule.EnumValue = text.ModuleName; Context.TEModules.AddObject(teModule); //Context.SaveChanges(); TEModule aux = Context.TEModules.Where(module => module.EnumValue.Equals(teModule.ModuleName)).FirstOrDefault(); }
My problem is that if I save the SaveChanges comment, then the next time the aux object is always null, because Context.TEModules is empty even when I call AddObject . However, if I call SaveChanges after AddObject, then on the next request, the aux object will not be null. The problem is that I donโt want to call SaveChanges so often, because this is not the only piece of code into which I add objects, and performance decreases if I do this.
So the question is: do I need to ramp SaveChanges after every call to AddObject, if later I need to know if this object exists?
source share