5

When I execute an update on my SQL Server database, I get a result of the rows affected by the update AND the affected rows of some triggers.

So for example, an update executed directly on database:

UPDATE: (32 row(s) affected)
Trigger1: (1 row(s) affected)
Trigger2: (2 row(s) affected)
...

Now when I execute _context.Database.ExecuteSqlCommand(query, params) I always get the sum of all those results, in my example the result value is 35.

I only need the result of the UPDATE, in my example 32.

Is there any possibility to ignore the results of the triggers?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Obl Tobl
  • 5,604
  • 8
  • 41
  • 65

2 Answers2

3

Put SET NOCOUNT ON on the first line of your triggers.

Alireza
  • 4,976
  • 1
  • 23
  • 36
2

I think @Alireza's answer makes the most sense if it's possible to change the triggers but if it's not could you change your database call to execute the update statement and return @@ROWCOUNT?

_context.Database.SqlQuery<int>("update xx ...; select @@ROWCOUNT");

I can't find any documentation on MSDN but this question confirms that @@ROWCOUNT is unaffected by triggers.

Community
  • 1
  • 1
petelids
  • 12,305
  • 3
  • 47
  • 57