The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Error on Save: "You must call Save() on the esEntityCollection"

Last post 03-29-2007, 9:07 PM by dzilla. 6 replies.
Sort Posts: Previous Next
  •  03-29-2007, 6:44 AM 1504

    Error on Save: "You must call Save() on the esEntityCollection"

    Here is my scenario:

    EntitySpaces Version # 2007.0.0328.0
    MyGeneration Version # 1.2.0.5
    Vista DB Version #3.20.1.20

    I have a collection which is loaded at runtime, then bound to a listview object.

    Code:
    1            private EmployeeCollection findEmployees()
    2            {
    3                //get initial EmployeeCollection object
    4                emps = new EmployeeCollection();
    5                
    6                //do the search and return the results
    7                emps.Query.Load();
    8    
    9                //sort results based on ddlSearchSortOrder control
    10               if (ddlSearchSortOrder.SelectedItem != null)
    11               {
    12                   ColumnHeader column = (ColumnHeader)ddlSearchSortOrder.SelectedItem;
    13                   emps.Sort = (string)column.Tag + " " + sortOrder;
    14               }
    15   
    16               return emps;
    17           }
    

     

    When a user double-clicks on the Employee, we open a new form by storing the current 'employee' in a variable, and passing it to the new form.

    Code:
    1            private void btnSelectEmployee_Click(object sender, EventArgs e)
    2            {
    3                //make sure we have a valid employee selected
    4                if (lvFoundEmployees.SelectedItems.Count > 0)
    5                {
    6                    _presenter.OnCloseView();
    7                    _presenter.OpenEmployeeForEdit((Employee)lvFoundEmployees.SelectedItems[0].Tag);
    8                }
    9                else
    10               {
    11                   _presenter.OnNoEmployeeSelected();
    12               }
    13           }
    

    Then, we allow the user to make some changes to the form, and click the save button, which calls our save method.

    Code:
    1    internal void OnSaveDraft_Click()
    2            {
    3                if (employee != null)
    4                {
    5                    //save employee to db with DRAFT status
    6                    employee.EmployeeStatusID = EmployeeStatus.DRAFT;
    7    
    8                    //Call EndEdit() for all editable DataBindingSources in child views.
    9                    _employeeDetailView.EndEditOnDataBindingSources();
    10                   _employeeJobcodeView.EndEditOnDataBindingSources();
    11   
    12                   //save the employee to the local DB
    13                   using (esTransactionScope scope = new esTransactionScope())
    14                   {
    15                       employee.EndEdit();
    16                       employee.Save();
    17                       scope.Complete();
    18                   } 
    19   
    20                   //notify the user of save success
    21                   string msg = String.Format(Resources.dialog_SaveEmployeeSuccessful_Draft, employee.ToString());
    22                   shellNotificationService.Show(msg, Resources.caption_SaveEmployeeSuccessful, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
    23   
    24                   //close the form
    25                   this.OnCloseView();
    26               }
    27           }
    

     

    The probem occurs on line #16 when we call the Save() method.  It throws an error which says "You must call Save() on the esEntityCollection".  For some reason the employee object is holding onto the rest of the collection when it is passed to the form. 

    1. Is there anyway to 'drop' the collection once the employee is passed to the 'edit' form?  I really don't want to make another call to the db if possible.
    2. If not, I was thinking about using the code below, which would basically call the employee.Save() if employee.collection is null, or otherwise call employee.collection.save().  However, is this only going to save the current employee, b/c its the only one which has been modified - or will it save the ENTIRE collection??

    BTW - It would be REALLY helpful if there was an easy way to change the text color in these posts (without editing the HTML) !!

  •  03-29-2007, 6:58 AM 1506 in reply to 1504

    Re: Error on Save: "You must call Save() on the esEntityCollection"

    This is working as designed, you cannot save a single entity that lives on it's own. You have two easy options however.

    1. Before you pass this single employee over to your other form for editing simply create a new Employee and call his LoadByPrimaryKey using the data from the guy in the collection and pass this new Employee object over the other forum. You will be able to save him since he doesn't live in a collection.
    2. call employee.Collection.Save() in your other form, but remember, you will save any changes made in the collection as well.



    As far as colors go there's no real reason to use them, just use bold or italics, posts get crazy tacky with color sometimes and detract from the actual text.

     


    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  03-29-2007, 7:00 AM 1508 in reply to 1504

    Re: Error on Save: "You must call Save() on the esEntityCollection"

    Two thoughts, there is the DetachEntity() method on collection to remove an entity from a collection. However I would just call save on the collection if its available, the save is smart it only saves changed records.

    Regards,

    Scott Schecter
    EntitySpaces | My Site
  •  03-29-2007, 7:18 AM 1509 in reply to 1508

    Re: Error on Save: "You must call Save() on the esEntityCollection"

    Wow! Thanks for the quick response.  I tried the es.Collection.Save() method, and it seemed not to save my other changed objects, could this be b/c .EndEdit() hasn't been called on those?  Also, can you give me a little more detail on the DetachEntity() method you mentioned? 

    My main concern is because our application allows the user to edit multiple employees; so I don't want to be saving changes for another employee which happens to be opened when they haven't requested the change.  This could cause MEGA problems!

     Also - as for the colors - you are correct, however, in the event of a cut and paste, there is no way to 'tix' crazy colors either.  It would at least be nice to have a way to make the next black.

  •  03-29-2007, 7:24 AM 1511 in reply to 1509

    Re: Error on Save: "You must call Save() on the esEntityCollection"

    Use my famous color stripper technique, copy into notepad, then into our posts !!

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  03-29-2007, 7:51 AM 1513 in reply to 1511

    Re: Error on Save: "You must call Save() on the esEntityCollection"

    Its hard to link to the API docs currently, we are working on this. Basically its a method on your collection that takes an entity that is a part of that collection and returns a standalone entity.
     


    Regards,

    Scott Schecter
    EntitySpaces | My Site
  •  03-29-2007, 9:07 PM 1532 in reply to 1508

    Re: Error on Save: "You must call Save() on the esEntityCollection"

    I ran into this scenario all the time in the very begining with Entity vs Collections & Save().  My little workaround if you will, was to add an overload Save() method in a custom template...such that it would test for the Collection.  I don't know if it would get in someone elses way or not - but I try my best to ensure I don't abuse it.  Calling .Save() on every item while iterating over some large collection wouldn't be the smartest thing to do. 

    Code:

    public override void Save()
    {
         if (this.Collection == null)
             base.Save();
         else
             this.Collection.Save();
    }
View as RSS news feed in XML