Database.ExecuteSqlCommand returns incorrect rowcount

dml entity-framework-6

Question

To update a table, I'm using context.Database.ExecuteSql. The record is updated and the update with where clause is correctly carried out. But rather than returning 1 for rowcount, the procedure returns 2. The result rowcount provided when I run the update command in SSMS is 1. Can somebody provide some light on this?

            string query =
            string.Format("update {0} set Description = '{1}', Code = '{2}', LastUpdatedBy = '{3}', LastUpdatedDate = '{4}' where ID = {5};",
                tableName,
                description,
                code,
                lastUpdatedBy,
                lastUpdatedDate,
                ID);

        int rowCount = 0;
        string message = string.Empty;

        using (DBContext context = new DBContext())
        {
            rowCount = context.Database.ExecuteSqlCommand(TransactionalBehavior.EnsureTransaction, query);
        }

        return rowCount == 0 ?  //this return 2 instead of 1.
            new SaveResult(SaveResult.MessageType.Error, string.Format("There was an error updating a record in the {0} table.", tableName), "Index") :
            new SaveResult(SaveResult.MessageType.Success, string.Format("The update of {0} was successful.", tableName), "Index");

Rowcount = 1 is returned by this in SSMS.

update zAddressTypes 
set Description = 'Current', Code = '101122', LastUpdatedBy = 'user', LastUpdatedDate = '10/20/2014 12:17:26 PM' 
where ID = 1; 

DECLARE @RowCount INTEGER = @@ROWCOUNT; 
select @RowCount;
1
1
10/20/2014 4:58:18 PM

Popular Answer

The number of rows is returned separately. The query's exit status is what you can observe in this area.

ExecuteSqlCommand return value is not a row count but rather the status of the query. To return the rowcount, you might want to consider utilizing a datareader or a comparable tool.

2
6/2/2015 8:08:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow