Error with new UpdateAll Feature

6 posts, 0 answers
  1. Braden
    Braden avatar
    12 posts
    Member since:
    Oct 2012

    Posted 28 Jun 2013 Link to this post

    I have been trying to experiment with the new UpdateAll extension method and it is just throwing an error while trying to run the update.  I have a generic class that allows operations on the different classes generated by the OpenAccess designer and added a generic UpdateAll function that takes an expression to filter on and then the updateDescription that the UpdateAll method is looking for.

    public virtual void UpdateAll(System.Linq.Expressions.Expression<Func<T, bool>> rowSelector, Expression<Func<ExtensionMethods.UpdateDescription<T>, ExtensionMethods.UpdateDescription<T>>> updateDescription)
    {
    using (var _entities = new Entities())
    {
    _entities.GetAll<T>().Where(rowSelector).UpdateAll(updateDescription);
    }

    The rowSelector that I am passing in is filtering down to a single element on the primary key and the updateDescription is chained to update 3 different fields:

    ContactLogRepository.UpdateAll(x => x.Id == id,
    x => x.Set(y => y.StatusId, y => 2)
    .Set(y => y.ModifiedBy, y => "Some Name")
    .Set(y => y.ModifiedOn, y => DateTime.Now));

    Using the profiler, the SQL that is generated appears like it should work:

    INSERT INTO #TMPBE81BDB03B154A72B54479AA2E (C0)
    SELECT a.[Id]
    FROM [dbo].[ContactLog] a
    WHERE a.[Id] = 1178435

    UPDATE
    x
    SET x.[StatusId]   = 2,
    x.[ModifiedBy] =
    'Some Name',
    x.[ModifiedOn] =
    '6/27/2013 4:53:17 PM'

    FROM
    [dbo].[ContactLog] x

    INNER
    JOIN #TMPBE81BDB03B154A72B54479AA2E t
    ON (x.[Id] = t.C0)

    However, it throws an error while executing:

    "Failure: Batch Update failed on server: Expected=1/Got=2"

    Any thoughts?

  2. Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 28 Jun 2013 Link to this post

    Hi Braden,

    The reason for this exception is that OpenAccess ORM detects that more than one record is updated. This could be caused because of the additional rows affected by trigger under the table you update.

    Please validate that this is the case for your model so that we can address this issue in our next releases. 
     

    Regards,

    Kaloyan Nikolov
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
  3. DevCraft banner
  4. Braden
    Braden avatar
    12 posts
    Member since:
    Oct 2012

    Posted 28 Jun 2013 Link to this post

    There is indeed a trigger on the table that I was trying to update that inserts a row into a history table.  Will that be fixed in a future release?
  5. Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 02 Jul 2013 Link to this post

    Hi Braden,

    This is the designed behavior for our bulk operation API. We check the actual rows affected with the expected row count and if they are not equal OpenAccess ORM rolls back the transaction, especially when you perform operation based on the primary key. This is done as a protection against unexpected updates/deletes.
    I would say that the correct way to handle this situation is to SET NOCOUNT ON in your triggers, if you do that you still will be able to benefit from this protection check. 

    Example trigger:
    ALTER TRIGGER [dbo].[TaskUpdated]
    ON [dbo].[Task]
    AFTER UPDATE
    AS
    Begin
      SET NOCOUNT ON
      INSERT into modifications ([TimeOfChange]) values(getdate())
    end

    At the time being we do not plan to change the behavior of the UpdateAll/DeleteAll bulk operations because of those of our customers that use this check to prevent unexpected updates/deletes.

    I hope this helps. Please do not hesitate to get back to us if you have any further questions.
     

    Regards,
    Kaloyan Nikolov
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
  6. Braden
    Braden avatar
    12 posts
    Member since:
    Oct 2012

    Posted 02 Jul 2013 Link to this post

    Setting nocount to on does indeed resolve the issue.  Is there a place where these API methods are documented?  It would be helpful to be able to reference documentation, especially for new features, instead of having to post on the forum.

    Thanks!
  7. Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 04 Jul 2013 Link to this post

    Hi Braden,

    here you can find the Telerik OpenAccess ORM Bulk operations documentation including articles for the Bulk Delete and for the Bulk Update methods.
     

    Happy coding,
    Kaloyan Nikolov
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Back to Top
DevCraft banner