EF4 Stress Problems RRS feed

  • Question

  • Hello,

    I am using EF4 and loading a few records (around 80):

    IEnumerable<Job> jobs = _jobRepository.Find(job => job.Date == date);

    After testing this code on the server with some data I get huge delays and memory usage peaks.

    Each job has two binary fields (Filestream): Paper and Report

    My idea is that the files might being loaded and create this problem. So I tried the following:


       return _jobRepository.Find(j => j.Date == date)
        .Select(j => new Job {
         Date = j.Date,
         Area = j.Area,
         Closed = j.Closed,
         Devices = j.Devices,
         Project = j.Project


    The problem is that in this case I get an error:

    The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph.

    If I remove "Devices = j.Devices" then I don't get this error anymore.

    1 - How can I solve this?

    2 - Is my idea of the binary files correct?

    3 - Should I create an index on date?

    Thank You,



    Thursday, June 23, 2011 12:19 AM


  • Hello,

    it looks like your Find is already returning IEnumerable, isn't it? In such case projection will not help you because EF will always have to load the whole entity and make projection in memory. You must do the projection on IQueryable so it is performed in the database. When doing projection on queryable you will not be able to project to mapped entity (Job) - it is not allowed. You must project to anonymous or custom type. Perhaps you should modify your model and introduce table splitting to store binary data in separate entity (and load them only if you need). In such case you will not need projection at all and you will be able to lazy load related entity with binary data.

    Btw. EF is not taking advantage of Filestreams - it access them as varbinary(max) fields. If you really want to use data streaming advantage you must use plain ado.net.

    Best regards,

    • Marked as answer by Jackie-Sun Monday, July 4, 2011 5:10 AM
    Thursday, June 23, 2011 8:18 AM