none
Entity Validation

    Question

  • Hi,

    I am trying to add some validation that ensure that the same machine cannot have two allocation periods which overlap, so far I have the following:

            Private Sub Allocation_Validate(entity As AllocationItem, results As EntitySetValidationResultsBuilder)
    
                If Me.Allocation IsNot Nothing Then
    
                    Dim OverlappingAllocations = From a In Me.Allocation
                                             Where entity.Id <> a.Id _
                                             AndAlso entity.MachineItem.Id = a.MachineItem.Id _
                                             AndAlso entity.StartDate >= a.StartDate _
                                             AndAlso (entity.StartDate <= a.EndDate Or a.EndDate Is Nothing)
    
                    If OverlappingAllocations.any Then
                        results.AddEntityError("Overlapping Allocations")
                    End If
    
                End If
    
            End Sub

    Unfortunately the 'OverlappingAllocation.any' fails as is it not a member. How do I correctly write this?

    Thanks in advance for any help.

    mardi 13 mars 2012 14:19

Réponses

  • hey Graham

    is ALLOCATION table "child" table?    of lets say MACHINE table?

    then instead of Me.Allocation, You should try with

    entity.MACHINE.Allocation   and it should work...

    i hope i am not giving YOu headache!   :-)

    mardi 13 mars 2012 15:00

Toutes les réponses

  • Graham,

    i am not in front of LS now, but You can try .Count instead of .Any    (.Count >0 or .Count >1, check what does better job for You.)

    hope this helps.

    regards,

    Miroslav

    mardi 13 mars 2012 14:23
  • Hi Graham,

    What is the type of Me.Allocation?  Is it an EntityCollection of some type, is it a Query, or is it something else?

    mardi 13 mars 2012 14:27
  • Graham,

    also - if this is SERVER tier Validation, i believe You should change Me.Allocation to entity.Allocation,

    maybe i am not right, but great if you could check on that.

    Cheers

    mardi 13 mars 2012 14:32
  • Thanks for the prompt responses.

    @Miroslav - when trying .count I get '.count is not a member of 'Microsoft.Lightswitch.IDataServiceQueryable(Of LightswitchApplication.AllocationItem''

    @snomis - does the above information help?  I believe its an entity collection.

    @Miroslav - yes I believe it is server ties as it in entity validation rather than property validation.  Changing me.allocation to entity.allocation fails as .allocation is not a field within entity.

    mardi 13 mars 2012 14:38
  • Graham,

    it should work, both for Count and Any, but i am pretty much You should use entity.??? rather than Me.??? in entity validation.

    OK, "Allocation" is table name, and in Your business logic You should work with table fields that entity. is having.

    i dont know if this helps it all....  :)    but what you are trying to achieve is nearly working, so i would suggest You try using only entity. and bypass Me. for this function.

    Cheers

    mardi 13 mars 2012 14:52
  • Graham,

    It doesn't seem like Me.Allocation is an entity collection.  I say that because it appears that that your validate method is the EntitySet validator on the service tier as Miroslav indicated earlier.  That means Me is the DataService which cannot contain entity collection members unless you added one in your parital class.  The behavior you are getting seems to indicate Me.Allocation is a query or EntitySet.  If this is really what you want, you will not be able to use an Any operator as it is not supported in LightSwitch queries.  Instead you can use the FirstOrDefault operator and check for null.

                    If OverlappingAllocations.FirstOrDefault() is Not Nothing Then
                        results
    .AddEntityError("Overlapping Allocations")
                   
    End If

    mardi 13 mars 2012 14:57
  • hey Graham

    is ALLOCATION table "child" table?    of lets say MACHINE table?

    then instead of Me.Allocation, You should try with

    entity.MACHINE.Allocation   and it should work...

    i hope i am not giving YOu headache!   :-)

    mardi 13 mars 2012 15:00
  • On the contrary Miroslav, not solving this is giving me a headache, but I believe you have relieved my headache.

    That is now working, many thanks!

            Private Sub Allocation_Validate(entity As AllocationItem, results As EntitySetValidationResultsBuilder)
    
                If Me.Allocation IsNot Nothing Then
    
                    Dim OverlappingAllocations = From a In entity.MachineItem.Allocation
                                             Where a IsNot entity _
                                             AndAlso (entity.StartDate >= a.StartDate And (entity.StartDate <= a.EndDate Or a.EndDate Is Nothing))
    
                    If OverlappingAllocations.Any Then
                        results.AddEntityError("The allocation start and end date overlaps another allocation start and end date for the same item")
                    End If
    
                End If
    
            End Sub

    mardi 13 mars 2012 15:36