Entity Framework request outofmemoryexception

I am new to the .NET Entity Framework and try to iterate over records in a table with several million rows. Here is the basic code:

// select the records from the database var records = from data in dataContext.Messages select data; // iterate over the messages foreach (var record in records) { // do nothing } 

When repeating the data, I get an "outofmemoryexception". Is there a way to modify my query or manage the memory of an ObjectQuery instance?

+6
source share
2 answers

I suspect the problem is that the Entity Framework is trying to cache / track all this data in the context of your object, which ultimately leads to an OutOfMemory Exception if the data set is huge.

You can disable the shutdown manually to avoid this:

 dataContext.Messages.MergeOption = System.Data.Objects.MergeOption.NoTracking; 

The allocated memory that is currently located in the data context - this memory will eventually receive garbage collection after you place the context, so that you can materialize smaller batches of lines inside the used block or manually delete the object context to return memory between each batch .

+4
source

As for modifying your query, you can add a where clause to update the returned records:

http://msdn.microsoft.com/en-us/library/bb311043.aspx

0
source

Source: https://habr.com/ru/post/893632/


All Articles