none
Entity Framework SaveChanges() RRS feed

  • Question

  • Hi,

       I am getting an issue where I am updating a collection and then saving the collion back to the database, the issue is that once the save had been completed the collection appears to now be empty (no objects in the collection any more).

    The collection saves okay and the data in the database is actually updated but th4e collection now has no objects??

    Has anyone had this type of issue before and if so how do I resolve it??

    Regards..

    Peter

    Wednesday, May 9, 2012 6:23 AM

Answers

  • Peter,

    Sorry, I didn't realize that the problem was with the _schedules variable. The problem is most likely due to lazy execution. The _schedules variable doesn't really "contain" the entities in the sense that a List(Of T) or an Array do, it's simply a reference which when enumerated calls back to the database to retrieve the requested information. Note that in your code you're making three calls to the database, once for each call to .Count() and then once for the enumeration.

    Wherever you may be accessing the contents of _schedules again further along in your code you'll be causing another call to the database. At that time something may have changed which would yield no results using the same query. I can't say for sure without seeing the contents of the JaixDB.TaskSchedules.SelectSchedules(...) method and wherever it is that you're using the _schedules variable further along.

    If you want to avoid more than one call to the database and maintain the _schedules as a static list of in memory objects then you should type it as an Array or List and call JaixDB.TaskSchedules.SelectSchedules(...).ToList() or .ToArray() when assiging it.

    Regards,

    Tyler

    • Marked as answer by PeterA56 Thursday, May 10, 2012 1:24 AM
    Thursday, May 10, 2012 1:01 AM

All replies

  • Hi PeterA56,

    Welcome to MSDN Forum.

    Could you please post some code here? This is, so I can help you more effectively.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 9, 2012 7:43 AM
    Moderator
  • Hi,

    It's likely better to describe what you have rather than to hope for someone that had the exact same issue. An issue I saw several time is overwriting the deployed database file with an empty database file stored in the project. As a result the application appears to run fine but data are cleared on the next run because the database file is overwritten.

    What is the db you are using ? Are you using AttachDbFilename in your connection string ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Wednesday, May 9, 2012 9:16 AM
  • Allen,

         Thanks for the response, here is the calling code:

     

    Using context AsNew JaixDatabase.DatabaseObjects.JaixContext_UOWScope(True)                 Dim _schedules As System.Data.Objects.ObjectQuery(Of Model.TaskSchedule)                 Dim _taskSchedules As System.Data.Objects.ObjectQuery(Of Model.TaskSchedule)                 Dim _schedule As Model.TaskScheduleDim _report As Model.DocumentTemplateDim _history As Model.TaskScheduleHistoryDim _scheduledReport AsNewXtraReportDim objDatset AsIDXReportDataSet                 _schedules = JaixDB.TaskSchedules.SelectSchedules(Model.TaskSchedules.SelectScope.Active)                 If _schedules.Count > 0 Then                     _logWriter.WriteArgsLog(NewObject() {String.Format("Reports to be processed: {0} reports", _schedules.Count), vbCrLf})                     ForEach schedule As Model.TaskScheduleIn _schedules                         schedule.Processing = TrueNext                     JaixDB.TaskSchedules.SaveAllChanges()                     '**** WORK AROUND: Line below added as the SaveAllChanges() above appears to be clearing the collection??                     _taskSchedules = JaixDB.TaskSchedules.SelectUpdatedSchedules(Model.TaskSchedules.SelectScope.Active)

    Patrice,

    We are connecting to an SQL2008 server.

    Regards..

    Peter

                                                                                                    
    Wednesday, May 9, 2012 10:54 PM
  • Hi Peter,

    In the code you've posted you the variable _taskSchedules is declared, but never assigned before the call to SaveAllChanges(). Unless you've omitted something from your code then there's no reason to expect _taskSchedules to have any value until you've assigned it in the line following the call to SaveAllChanges().

    Regards,

    Tyler

    Thursday, May 10, 2012 12:15 AM
  • Tyler,

       Thanks for the response, the issue is that the _schedules collewction is being cleared after SaveAllChanges(), the _taskSchedules collection nbis being decleared and implemented as a work around for the _schedules issue.

    Regards Peter.

    Thursday, May 10, 2012 12:23 AM
  • Peter,

    Sorry, I didn't realize that the problem was with the _schedules variable. The problem is most likely due to lazy execution. The _schedules variable doesn't really "contain" the entities in the sense that a List(Of T) or an Array do, it's simply a reference which when enumerated calls back to the database to retrieve the requested information. Note that in your code you're making three calls to the database, once for each call to .Count() and then once for the enumeration.

    Wherever you may be accessing the contents of _schedules again further along in your code you'll be causing another call to the database. At that time something may have changed which would yield no results using the same query. I can't say for sure without seeing the contents of the JaixDB.TaskSchedules.SelectSchedules(...) method and wherever it is that you're using the _schedules variable further along.

    If you want to avoid more than one call to the database and maintain the _schedules as a static list of in memory objects then you should type it as an Array or List and call JaixDB.TaskSchedules.SelectSchedules(...).ToList() or .ToArray() when assiging it.

    Regards,

    Tyler

    • Marked as answer by PeterA56 Thursday, May 10, 2012 1:24 AM
    Thursday, May 10, 2012 1:01 AM
  • Tyler,

        Thanks for the update, I haven't done much with EF so I'm still in the learning stage. I'll keep in mind waht you have said.

    Regards..

    Peter 

    Thursday, May 10, 2012 1:24 AM