Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I am running into a problem when selecting a single record from our email database.
The record is over 25MB (estimated, probably more) in size and every time I try to get it from the context it throws an OutOfMemoryException.
NoTracking is unfortunately no option in this case in order to avoid the second copy.

The application copies emails to an archive database and I already set the batch size to 1 record per context before disposing and recreating the context (using statement)



Here is a stub for the function in question:

using (EmailDataEntities context = ContextFactory.GetEmailDataEntities())
{
    using (EmailArchiveDataEntities contextArchive = visualbooking.EmailArchiveDataContext.ContextFactory.GetEmailArchiveDataEntities())
    {
        foreach (long id in idList)
        {
            try
            {
                EmailDataContext.Model.tblEMailPostBox oDataEmail = context.tblEMailPostBox.FirstOrDefault(f => f.id == id);

                // copy the data here
                            

                // deleting original email


                // saving both contexts


                // Accepting all changes


            }
            catch (Exception ex)
            {
                // logging exception
            }
        }
    }
}



The exception is thrown in the .FirstOrDefault() part, copying and saving is working in all cases except in the case of the big record.
Right now I just skipped the record. It is possible to copy it manually in the database, but I'd like to know about the background causing the problem... or the limits of records if there are any...


Is there a way to get the record regardless of its size?
Or is there a workaround?

Any help is kindly appreciated,
have a great day
Andy
Posted
Updated 27-Mar-14 4:27am
v2
Comments
Oshtri Deka 27-Mar-14 10:21am    
Can we see some code?
What sort of application is this?
Service? WebApp?
hoernchenmeister 27-Mar-14 10:29am    
Hi Oshtri Deka,
thanks a lot for your interest in my question.
It's a Console App that will later be called by a Scheduler.
Right now I am testing it and experienced the problem mentioned above.
I am just trying to get a better overview about the possible impact of this (there might be more of those large records in the future) before taking it to the live environment.
Best regards
Andy
hoernchenmeister 27-Mar-14 10:29am    
I have added the relevant code stub :)
Maarten Kools 28-Mar-14 2:37am    
What does the property context.tblEMailPostBox represent? If I had to wager a guess, I'd say all records. When you execute FirstOrDefault, the IEnumerable will be iterated (which, by the way, is very inefficient in your loop over the ids). If this property contains all records then you might have a problem with memory, because it would load every single record in memory (no idea how many we're talking about here?) and from that collection you select only one.
hoernchenmeister 7-Apr-14 8:45am    
Dear Maarten,
sorry for being so late with my reply and thanks a lot for your help on this one.
That's a really good point you made here. the property represents indeed a complete table. The "id" field is the primary key for this table. I always assumed thet FirstOrDefault would be the right way to retrieve a single record (over SingleOrDefault). Would it be better to use a where statement first?
Like this:
EmailDataContext.Model.tblEMailPostBox oDataEmail = context.tblEMailPostBox.Where(f => f.id == id).FirstOrDefault();

have a great day
Andy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900