locked
Entity 'Inserted' method interfering with 'Validate' method? RRS feed

  • Question

  • In my HTML app I have two methods running on the server for an 'Employee' entity-- Validate and Inserted.  

    The 'Validate' method checks that an employee hasn't been added twice to the same department.  The 'Inserted' method sets a boolean flag on the Employee -- namely, Employee.IsNew = true.

    When that happens however, it appears to trigger another call to the Validate method, which then squawks because the employee has now been added twice to the same department?

    Is it the case that a call to the Inserted method will trigger a call to Validate?  And for my specific problem, should I set that boolean flag in the 'Inserting' method instead?

    Sunday, December 13, 2015 11:27 PM

Answers

  • Anytime you add or change an entity the validation code should run, although you should be able to eliminate that record from being detected as a duplicate in your validation query code.  Rather than refactor, you could try to do your operation in the _inserting method rather than the _inserted method.  That may solve your problem by merging all changes into a single transaction.
    • Proposed as answer by ADefwebserver Monday, December 14, 2015 4:49 AM
    • Marked as answer by jim bancroft Monday, December 14, 2015 7:33 AM
    Monday, December 14, 2015 3:21 AM

All replies

  • Thanks Michael.  So if I follow the diagram and Dan's article, making a change in the post-process ('inserted') section will re-trigger validation.  In my case, the Employee I just added is in the entity set (though not committed yet), and therefore validation will squawk because it will find a duplicate in the set.  Does that sound right?
    Monday, December 14, 2015 2:43 AM
  • Anytime you add or change an entity the validation code should run, although you should be able to eliminate that record from being detected as a duplicate in your validation query code.  Rather than refactor, you could try to do your operation in the _inserting method rather than the _inserted method.  That may solve your problem by merging all changes into a single transaction.
    • Proposed as answer by ADefwebserver Monday, December 14, 2015 4:49 AM
    • Marked as answer by jim bancroft Monday, December 14, 2015 7:33 AM
    Monday, December 14, 2015 3:21 AM
  • That did the trick, thanks.  It would seem (someone correct me if I'm mistaken) that changes made in the _inserting method are in the same transaction, while those in the _inserted one are not.

    Out of curiosity, how would I go about preventing the new record from being detected as a duplicate in the validation code?  All I do in the validation is check whether the given entity (Employee) already exists in the Employees table.  I'm being bitten because the entry is apparently in the Employees table when validation kicks in the second time around, though the record hasn't been committed yet.  Would the entity itself have a flag that I could use to check?

    Monday, December 14, 2015 7:40 AM
  • Hello

    Sorry to arrive late to this one but I would negate the inserted record by making sure that the unique id on the table was not equal to the one that you are inserting (or isn't equal to 0). This is what I do on my stuff very successfully


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Monday, December 14, 2015 9:44 AM
  • Jim,

    This is the code I use to check for duplicates in the _validate method.  The code is partially adapted from similar code in the book: Pro Visual Studio LightSwitch 2011 Development by Yann Duran and Tim Leung.

    partial void Status_Validate(EntityValidationResultsBuilder results)
    {
        if (String.IsNullOrEmpty(this.Status))
        {
            results.AddPropertyError("Status: Please enter a Status.");
        }
        else
        {
            bool duplicateOnServer = this.DataWorkspace.DataSource.Orders
                .Where(a => a.Status.Equals(this.Status, StringComparison.CurrentCultureIgnoreCase) && a.Id != this.Id)
                .FirstOrDefault() != null;
    
            var changes = this.DataWorkspace.DataSource.Details.GetChanges();
            
            // this part is for the SilverLight Client only - not necessary for html client
            bool duplicateOnClient = changes.OfType<Order>()
                .Where(a => a.Status.Equals(this.Status, StringComparison.CurrentCultureIgnoreCase) && a != this)
                .Except(changes.DeletedEntities).FirstOrDefault() != null;
    
            if (duplicateOnServer || duplicateOnClient)
            {
                results.AddPropertyError(String.Format("Status: {0} is a duplicate entry.", this.Status));
            }
        }
    }

    Monday, December 14, 2015 5:34 PM