locked
Incremental save with entity framework RRS feed

  • Question

  • My application uses EF as data model and we are handling a large database.  Is there a way to do incremental save with entity?  Right now, each time user press "Save" button, it saves to whole entity to database which is a big performance issue for us.  

    What should I look in order to improve performance since we are handling large amount of data.  There are not many tables but each table has a lot of rows. 

    Wednesday, March 20, 2013 7:19 PM

Answers

  • Well you can look at the ObjectStateManager to find out how many objects have changes although if you are saying that all entities are being updated to the database you will find that they all have been set in the ObjectStateManage.

    You can have the debugger break when a value in the ObjectContext / DbContect / ObjectStateManager has change. Take a look at this web page, "Breakpoints: Use Hit Counts, Call Stack Functions, and Conditions to Break When and Where You Want in the Visual Studio Debugger", more specifically Specify a breakpoint condition using a code expression, to see how to do that in the debugger. 

    And of course you should be checking with the third-party software vendor.

      


    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 mysti789 Tuesday, July 30, 2013 4:00 PM
    Tuesday, July 23, 2013 4:09 PM

All replies

  • Hi mystique99;

    The way the EF model works is that only those entities in the data context that have been modified since the last time they have been retrieved from the database will be persisted back to the database.

    So if you have 1000 records in the data context and you modified one record and then do a save changes then only one record will be sent to the database. If you have 100 records of the 1000 in the data context that are modified then 100 records will be persisted back to the database.

    Are you not seeing this behavior?

      


    Fernando (MCSD)

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

    Wednesday, March 20, 2013 8:31 PM
  • Thanks for your reply.

    I am not sure since my save operation takes a long time.  We do use a third party graphics library and I am not sure whether that makes it takes a long time.  What is the best way to find out? 

    Thursday, March 21, 2013 2:17 AM
  •  

    To your statement, "Right now, each time user press "Save" button, it saves to whole entity to database which is a big performance issue for us.", and how have you determined that?

    To your statement, "We do use a third party graphics library and I am not sure whether that makes it takes a long time.", graphics library should not be causing all records to be sent to the database on SaveChanges call.

    Can you post the code that is causing the issue?

      


    Fernando (MCSD)

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

    Thursday, March 21, 2013 3:16 AM
  • We used the SQL profiler to log the transactions and verified that entity is doing a complete save even we have changed no data.  My question is:  how to do an incremental save with entity?
    Thursday, July 18, 2013 7:41 PM
  • And what do you mean by an incremental change? As I stated four months ago Entity Framework does not update the database unless it retrieved records to the local system and those records have been modified and only then when a SaveChanges method has been called.

    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.

    Thursday, July 18, 2013 7:50 PM
  • Our data model contains a lot of entities.  Ideally, if we change one entity and call SaveChanges, it should generate SQL transactions that save only this entity.  That is what I mean incremental save.  However, we found that calling SaveChanges generat sql statements that save all entities in the model (by using SQL profiler to monitor the sql server actions), including entities that have not been changed.  Our coding may have some problems.  

    Friday, July 19, 2013 12:15 PM
  • Hi 

    You state the following, "Our coding may have some problems.", in order to develop a solution for this question we need to see the code. From when you instantiate the data context to the point where you do the call to the SaveChanges method.

      


    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, July 19, 2013 6:09 PM
  • Thanks for your reply.  I am sorry that I forgot to mention that our project is actually a Silverlight 4 project using RIA service with entity framework.  We are calling the RIA DomainService DomainContext's submitChanges method to save changes to the database.  Will this make it save everything by default?  Is there a way to do incremental save in this case?


    • Edited by mysti789 Saturday, July 20, 2013 3:43 PM Typo
    Saturday, July 20, 2013 3:12 PM
  • To your question, "Will this make it save everything by default?", Entity Framework does NOT save records back to the database on SaveChanges unless one or more columns in a record has been modified. For all records in the data context to be sent to the database on SaveChanges each of those records must have one or more columns modified.

    You need to check your code to make sure you are NOT modifying the records after they are retrieved from the database. For example if in a table you have a column called LastAccessed and when you query the table and receive 1000 records and you modify that column with the current date on each of the 1000 records then all the records will be sent to the database.  


    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.

    Saturday, July 20, 2013 3:30 PM
  • I cannot get the code to you since our project is complicated.  We are using a third party tool to draw a hierarchal organization diagram using entity as the DAL.  Each organization node has a context menu allow user to open up different child windows to attach or modify other properties of the node.  We don't have any code modifying the entity intentionally when project is loaded initially.   However, the application does do a whole save to the database if we just do a load and then a save without doing anything else.   Do you see any entity property get modified in this case?  Which fields or settings should we look for in the edmx model to find out where we didn't do right?  




    • Edited by mysti789 Monday, July 22, 2013 2:05 PM typo
    Monday, July 22, 2013 1:55 PM
  • Well seeming that the Entity Framework standard operating procedure is to only send records back to the database when they have been modified and only then when SaveChanges has been called on the data context. I would think then that your third party designer may be the culprit and I would recommend you contacting the third-party software company to see what they are doing that maybe causing this issue.


    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.

    Monday, July 22, 2013 5:40 PM
  • Is there any tools you can recommend to help trouble shoot this problem? 
    Tuesday, July 23, 2013 3:25 PM
  • Well you can look at the ObjectStateManager to find out how many objects have changes although if you are saying that all entities are being updated to the database you will find that they all have been set in the ObjectStateManage.

    You can have the debugger break when a value in the ObjectContext / DbContect / ObjectStateManager has change. Take a look at this web page, "Breakpoints: Use Hit Counts, Call Stack Functions, and Conditions to Break When and Where You Want in the Visual Studio Debugger", more specifically Specify a breakpoint condition using a code expression, to see how to do that in the debugger. 

    And of course you should be checking with the third-party software vendor.

      


    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 mysti789 Tuesday, July 30, 2013 4:00 PM
    Tuesday, July 23, 2013 4:09 PM
  • I fonnd out what is the problem.  It is our code.  You are right, entity does not send everything back to database.  It only send those modified, added or delete chnages to database.

    thank you for your help.

    Tuesday, July 30, 2013 4:00 PM
  •   

    Great to hear, glad you have found the cause of the issue. Good luck and have a great day.

      


    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.

    Tuesday, July 30, 2013 4:03 PM