开发者

Does the entity framework ObjectContext maintain references after iterating over the results

the use case that I'm concerned with in this post involves iterating over a large number of entities (100K+) returned from a query.

Given the following code snippet:

var query = from c in context.Customers select c;
foreach(var customer in query)
    printCustomerStatement(customer);

In this example it's clear that the the customer instance is not needed after the call to printCustomerStatement. Will the ObjectContext be keeping 开发者_运维问答a reference to it regardless? My expectation is that it would not. and that this foreach would behave like a forward-only read-only enumerator method call.


Depends on the query MergeOption.

If you do this:

context.Customers.MergeOption = MergeOption.NoTracking;
var query = from c in context.Customers select c;
foreach(var customer in query)
    printCustomerStatement(customer);

...then the context won't store those references.

With the default MergeOption of AppendOnly, it will.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜