From 80973bb3c808e03f6ec1e2a01856662425d9504f Mon Sep 17 00:00:00 2001 From: Christopher Jolly Date: Mon, 9 Oct 2023 00:02:31 +0800 Subject: [PATCH] When generating the SELECT, to select the values of the newly inserted row we only are able to use the first key column with @@identity --- .../Update/Internal/JetUpdateSqlGenerator.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/EFCore.Jet/Update/Internal/JetUpdateSqlGenerator.cs b/src/EFCore.Jet/Update/Internal/JetUpdateSqlGenerator.cs index f37b7ac..862d975 100644 --- a/src/EFCore.Jet/Update/Internal/JetUpdateSqlGenerator.cs +++ b/src/EFCore.Jet/Update/Internal/JetUpdateSqlGenerator.cs @@ -105,5 +105,45 @@ namespace EntityFrameworkCore.Jet.Update.Internal return ResultSetMapping.LastInResultSet; } + + //If multiple columns were output, the SQL Server behavior is to produce a INSERT INTO ... OUTPUT statement + //Jet does not support OUTPUT, so we need to use a SELECT statement instead + //@@identity is available to get the identity value of the last inserted row. + //Most tables will only have one identity column, so the AppendIdentityWhereColumn only gets called once + //However if there is a complex identity so that you have more than one identity column, the rest of those get added + //Given @@identity only gets the value of the first identity column, we must only use that and not any others + + protected override void AppendWhereAffectedClause(StringBuilder commandStringBuilder, IReadOnlyList operations) + { + commandStringBuilder + .AppendLine() + .Append("WHERE "); + + AppendRowsAffectedWhereCondition(commandStringBuilder, 1); + bool isfirstkeycolumn = true; + if (operations.Count > 0) + { + commandStringBuilder + .Append(" AND ") + .AppendJoin( + operations, (sb, v) => + { + if (v is { IsKey: true, IsRead: false }) + { + AppendWhereCondition(sb, v, v.UseOriginalValueParameter); + return true; + } + + if (IsIdentityOperation(v) && isfirstkeycolumn) + { + AppendIdentityWhereCondition(sb, v); + isfirstkeycolumn = false; + return true; + } + + return false; + }, " AND "); + } + } } }