【发布时间】:2014-12-15 17:59:40
【问题描述】:
我正在使用 context.Database.ExecuteSql 来更新表。正确执行带有 where 子句的更新并更新记录。但是,该方法为 rowcount 返回 2 而不是 1。当我在 SSMS 中执行 update 语句时,返回的结果 rowcount 为 1。有人可以对此提供见解吗?
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");
这会在 SSMS 中返回 rowcount = 1:
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;
【问题讨论】: