This is a migrated thread and some comments may be shown as answers.

Error with new UpdateAll Feature

5 Answers 71 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Braden
Top achievements
Rank 1
Braden asked on 28 Jun 2013, 01:44 PM
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?

5 Answers, 1 is accepted

Sort by
0
Kaloyan Nikolov
Telerik team
answered on 28 Jun 2013, 03:49 PM
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.
0
Braden
Top achievements
Rank 1
answered on 28 Jun 2013, 03:55 PM
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?
0
Kaloyan Nikolov
Telerik team
answered on 02 Jul 2013, 07:57 AM
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.
0
Braden
Top achievements
Rank 1
answered on 02 Jul 2013, 01:23 PM
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!
0
Kaloyan Nikolov
Telerik team
answered on 04 Jul 2013, 06:29 AM
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.
Tags
Development (API, general questions)
Asked by
Braden
Top achievements
Rank 1
Answers by
Kaloyan Nikolov
Telerik team
Braden
Top achievements
Rank 1
Share this question
or