locked
ObjectStateManager.DetectChanges() performance issues RRS feed

  • Question

  • Hi All,

    We are using Entity Framework 4.0 with POCO in our application. Currently we are working on performance tuning of our application. We observed the performance issues for DetectChanges() function of EF. Below is the  case:

     I have loaded 2000 entities, all dependent entity's are also loaded. First time when I save it took 1.5 Seconds. Then I changed some fields of the entity and Save again. It took 4 Minutes. I profile the workflow and took snapshot, it shows the ObjectStateManager.DetectChanges() took 4 minutes to perform the application. See below snapshot:

    

    I am doing something horribly wrong here? Is there are issue with the EF?? Any workaround? Please suggest me the solution. This is very urgent!!!

    -Ajit

    Thursday, June 20, 2013 11:31 AM

Answers

  • Hi Ajit;

    Not knowing how the data context is configured and what you are doing with the 2000 entities in code it is difficult to say what is causing the long time to process the entities. Please have a look at this documentation and maybe it will help.

    Entity Framework Automatic Detect Changes

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Ajit Porlekar Monday, June 24, 2013 6:05 AM
    Thursday, June 20, 2013 2:40 PM
  • Sorry, i again profile the application and I come to know that DetectChanges() is called for every entity which was not necessary. Now I have removed the DetectChanges() from loop and it works.... not it takes 3 seconds to save the data.

    Thanks,
    -Ajit

    • Marked as answer by Ajit Porlekar Friday, June 21, 2013 7:05 PM
    Friday, June 21, 2013 11:20 AM

All replies

  • Hi Ajit;

    Not knowing how the data context is configured and what you are doing with the 2000 entities in code it is difficult to say what is causing the long time to process the entities. Please have a look at this documentation and maybe it will help.

    Entity Framework Automatic Detect Changes

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Ajit Porlekar Monday, June 24, 2013 6:05 AM
    Thursday, June 20, 2013 2:40 PM
  • Sorry, i again profile the application and I come to know that DetectChanges() is called for every entity which was not necessary. Now I have removed the DetectChanges() from loop and it works.... not it takes 3 seconds to save the data.

    Thanks,
    -Ajit

    • Marked as answer by Ajit Porlekar Friday, June 21, 2013 7:05 PM
    Friday, June 21, 2013 11:20 AM
  • Hi Ajit;

    Glad that worked out for you and now the performance is as expected.

    Please come back to this post and mark the post that help by clicking "Mark As Answer" on that post and "Mark as Helpful".

    Thank you.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, June 21, 2013 5:32 PM
  • Hi Ajit;

    Did not the post I posted lead you to the correct solution? If so did you mark the wrong post as the solution?

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, June 21, 2013 7:08 PM