Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UpdateExpression.VisitChildren to visit the setter column #33948

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,22 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
for (var (i, n) = (0, ColumnValueSetters.Count); i < n; i++)
{
var columnValueSetter = ColumnValueSetters[i];
var newColumn = (ColumnExpression)visitor.Visit(columnValueSetter.Column);
var newValue = (SqlExpression)visitor.Visit(columnValueSetter.Value);

if (columnValueSetters != null)
{
columnValueSetters.Add(columnValueSetter with { Value = newValue });
columnValueSetters.Add(new ColumnValueSetter(newColumn, newValue));
}
else if (!ReferenceEquals(newValue, columnValueSetter.Value))
else if (!ReferenceEquals(newColumn, columnValueSetter.Column) || !ReferenceEquals(newValue, columnValueSetter.Value))
{
columnValueSetters = new List<ColumnValueSetter>(n);
for (var j = 0; j < i; j++)
{
columnValueSetters.Add(ColumnValueSetters[j]);
}

columnValueSetters.Add(columnValueSetter with { Value = newValue });
columnValueSetters.Add(new ColumnValueSetter(newColumn, newValue));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ await AssertTranslationFailedWithDetails(
RelationalStrings.ExecuteDeleteOnTableSplitting(nameof(Owner)));
}

[ConditionalTheory] // #33937, #33946
[MemberData(nameof(IsAsyncData))]
public virtual async Task Replace_ColumnExpression_in_column_setter(bool async)
{
var contextFactory = await InitializeAsync<Context28671>();

await AssertUpdate(
async,
contextFactory.CreateContext,
ss => ss.Set<Owner>().SelectMany(e => e.OwnedCollections),
s => s.SetProperty(o => o.Value, "SomeValue"),
rowsAffectedCount: 0);
}

protected class Context28671(DbContextOptions options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public override async Task Delete_aggregate_root_when_table_sharing_with_non_own
AssertSql();
}

public override async Task Replace_ColumnExpression_in_column_setter(bool async)
{
await base.Replace_ColumnExpression_in_column_setter(async);

AssertSql(
"""
UPDATE [o0]
SET [o0].[Value] = N'SomeValue'
FROM [Owner] AS [o]
INNER JOIN [OwnedCollection] AS [o0] ON [o].[Id] = [o0].[OwnerId]
""");
}

public override async Task Update_non_owned_property_on_entity_with_owned(bool async)
{
await base.Update_non_owned_property_on_entity_with_owned(async);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Data.Sqlite;

namespace Microsoft.EntityFrameworkCore.BulkUpdates;

#nullable disable
Expand Down Expand Up @@ -59,6 +61,20 @@ public override async Task Delete_aggregate_root_when_table_sharing_with_non_own
AssertSql();
}

public override async Task Replace_ColumnExpression_in_column_setter(bool async)
{
// #33947
await Assert.ThrowsAsync<SqliteException>(() => base.Replace_ColumnExpression_in_column_setter(async));

AssertSql(
"""
UPDATE "OwnedCollection" AS "o0"
SET "Value" = 'SomeValue'
FROM "Owner" AS "o"
INNER JOIN "OwnedCollection" AS "o0" ON "o"."Id" = "o0"."OwnerId"
""");
}

public override async Task Update_non_owned_property_on_entity_with_owned(bool async)
{
await base.Update_non_owned_property_on_entity_with_owned(async);
Expand Down