I insert a lot of records into an Entity Framework database and don’t need them any more after I run .SaveChanges();. Moreover, I actually would like the memory to be released as soon as possible (for new records to be created). I don’t keep any variables linking to old records objects but the GC doesn’t seem to delete them, so, I think, it is the data context or something else who keeps them alive. How do I dispose them correctly to let GC to free memory they occupy?
I insert a lot of records into an Entity Framework database and don’t need
Share
SaveChanges()does not release the resources, it just commits the changes to the database.To release the resources you need to call
Dispose()method of the object context.It can be done explicitly by calling
.Dispose()method or by putting your code inside theusingstatement:Also,
ObjectContext.Dispose()method closes the connection if it was opened by the Entity Framework, otherwise you can run out of the connections…Here is a similar question: Should Entity Framework Context be Put into Using Statement?