diff --git a/src/EFCore.Relational/Update/UpdateSqlGenerator.cs b/src/EFCore.Relational/Update/UpdateSqlGenerator.cs
index 3496434b37c..ba53a3af12a 100644
--- a/src/EFCore.Relational/Update/UpdateSqlGenerator.cs
+++ b/src/EFCore.Relational/Update/UpdateSqlGenerator.cs
@@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore.Diagnostics;
+using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;
@@ -499,19 +500,32 @@ protected virtual void AppendWhereAffectedClause(
{
if (v.IsKey)
{
- if (v.IsRead)
- {
- AppendIdentityWhereCondition(sb, v);
- }
- else
+ if (!v.IsRead)
{
AppendWhereCondition(sb, v, v.UseOriginalValueParameter);
+ return true;
}
}
+
+ if (IsIdentityOperation(v))
+ {
+ AppendIdentityWhereCondition(sb, v);
+ return true;
+ }
+
+ return false;
}, " AND ");
}
}
+ ///
+ /// Returns a value indicating whether the given modification represents an auto-incrementing column.
+ ///
+ /// The column modification.
+ /// if the given modification represents an auto-incrementing column.
+ protected virtual bool IsIdentityOperation(IColumnModification modification)
+ => modification.IsKey && modification.IsRead;
+
///
/// Appends a WHERE condition checking rows affected.
///
diff --git a/src/EFCore.SqlServer/Update/Internal/SqlServerUpdateSqlGenerator.cs b/src/EFCore.SqlServer/Update/Internal/SqlServerUpdateSqlGenerator.cs
index 5bb527ad384..5f24d6e050b 100644
--- a/src/EFCore.SqlServer/Update/Internal/SqlServerUpdateSqlGenerator.cs
+++ b/src/EFCore.SqlServer/Update/Internal/SqlServerUpdateSqlGenerator.cs
@@ -295,10 +295,10 @@ private void AppendMergeCommandHeader(
.Append('(')
.AppendJoin(
writeOperations,
- toInsertTableAlias,
- SqlGenerationHelper,
- (sb, o, alias, helper) =>
+ (toInsertTableAlias, SqlGenerationHelper),
+ static (sb, o, state) =>
{
+ var (alias, helper) = state;
sb.Append(alias).Append('.');
helper.DelimitIdentifier(sb, o.ColumnName);
})
diff --git a/src/Shared/StringBuilderExtensions.cs b/src/Shared/StringBuilderExtensions.cs
index 781c193828a..eda4b05802e 100644
--- a/src/Shared/StringBuilderExtensions.cs
+++ b/src/Shared/StringBuilderExtensions.cs
@@ -45,20 +45,21 @@ public static StringBuilder AppendJoin(
return stringBuilder;
}
- public static StringBuilder AppendJoin(
+ public static StringBuilder AppendJoin(
this StringBuilder stringBuilder,
IEnumerable values,
- TParam param,
- Action joinAction,
+ Func joinFunc,
string separator = ", ")
{
var appended = false;
foreach (var value in values)
{
- joinAction(stringBuilder, value, param);
- stringBuilder.Append(separator);
- appended = true;
+ if (joinFunc(stringBuilder, value))
+ {
+ stringBuilder.Append(separator);
+ appended = true;
+ }
}
if (appended)
@@ -69,19 +70,18 @@ public static StringBuilder AppendJoin(
return stringBuilder;
}
- public static StringBuilder AppendJoin(
+ public static StringBuilder AppendJoin(
this StringBuilder stringBuilder,
IEnumerable values,
- TParam1 param1,
- TParam2 param2,
- Action joinAction,
+ TParam param,
+ Action joinAction,
string separator = ", ")
{
var appended = false;
foreach (var value in values)
{
- joinAction(stringBuilder, value, param1, param2);
+ joinAction(stringBuilder, value, param);
stringBuilder.Append(separator);
appended = true;
}