Say that I have the following code:
using (var db = new MyDatabaseContext())
{
foreach (var entity in db.LargeEntities)
{
byte[] data = entity.LargeBlob;
File.WriteAllBytes("c:\\" + entity.FileName);
}
}
When will the data for each entity.LargeBlob be ready for garbage collection? During the loop? After the using statement? I’m done with the objects after the WriteAllBytes line, so I’d like it to be disposed as soon as possible.
It is ready for gc after the
Usingblock. Because EF keeps a local copy of the loaded items you have to dispose Context to release the cached items.