Entity Framework 6: is there a way to iterate through a table without holding each row in memory

前端 未结 4 1185
悲&欢浪女
悲&欢浪女 2021-01-04 19:34

I would like to be able to iterate through every row in an entity table without holding every row in memory. This is a read only operation and every row can be discarded aft

4条回答
  •  北海茫月
    2021-01-04 20:10

    I haven't tested it, but try foreach (Quote L in context.Quotes.AsNoTracking()) {...}. .AsNoTracking() should not put entities in cache so I assume they will be consumed by GC when they out of the scope.

    Another option is to use context.Entry(quote).State = EntityState.Detached; in the foreach loop. Should have the similar effect as the option 1.

    Third option (should definitely work, but require more coding) would be to implement batch processing (select top N entities, process, select next top N). In this case make sure that you dispose and create new context every iteration (so GC can eat it:)) and use proper OrderBy() in the query.

提交回复
热议问题