Skip to content

Commit a4f32d1

Browse files
Fixed Update_complex_type_with_view_mapping tests
1 parent 0d1f0c1 commit a4f32d1

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteUpdate.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ bool TryTranslateMemberAccess(
300300
[NotNullWhen(true)] out IPropertyBase? property)
301301
{
302302
if (IsMemberAccess(expression, QueryCompilationContext.Model, out var baseExpression, out var member)
303-
&& _sqlTranslator.TryBindMember(_sqlTranslator.Visit(baseExpression), member, out var target, out var targetProperty))
303+
&& _sqlTranslator.TryBindMember(_sqlTranslator.Visit(baseExpression), member, out var target, out var targetProperty, forUpdate: true))
304304
{
305305
translation = target;
306306
property = targetProperty;
@@ -540,7 +540,7 @@ void ProcessComplexType(StructuralTypeShaperExpression shaperExpression, Express
540540
RelationalStrings.ExecuteUpdateOverJsonIsNotSupported(complexProperty.ComplexType.DisplayName()));
541541
}
542542

543-
var nestedShaperExpression = (StructuralTypeShaperExpression)projection.BindComplexProperty(complexProperty);
543+
var nestedShaperExpression = (StructuralTypeShaperExpression)projection.BindComplexProperty(complexProperty, forUpdate: true);
544544
var nestedValueExpression = CreateComplexPropertyAccessExpression(valueExpression, complexProperty);
545545
ProcessComplexType(nestedShaperExpression, nestedValueExpression);
546546
}
@@ -635,7 +635,7 @@ SqlParameterExpression parameter
635635
StructuralType: IComplexType,
636636
ValueBufferExpression: StructuralTypeProjectionExpression projection
637637
}
638-
=> projection.BindComplexProperty(complexProperty),
638+
=> projection.BindComplexProperty(complexProperty, forUpdate: true),
639639

640640
_ => throw new UnreachableException()
641641
};

src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,8 @@ public virtual bool TryBindMember(
12191219
Expression? source,
12201220
MemberIdentity member,
12211221
[NotNullWhen(true)] out Expression? expression,
1222-
[NotNullWhen(true)] out IPropertyBase? property)
1222+
[NotNullWhen(true)] out IPropertyBase? property,
1223+
bool forUpdate = false)
12231224
{
12241225
if (source is not StructuralTypeReferenceExpression typeReference)
12251226
{
@@ -1247,7 +1248,7 @@ public virtual bool TryBindMember(
12471248

12481249
if (complexProperty is not null)
12491250
{
1250-
expression = BindComplexProperty(typeReference, complexProperty);
1251+
expression = BindComplexProperty(typeReference, complexProperty, forUpdate: forUpdate);
12511252
property = complexProperty;
12521253
return true;
12531254
}
@@ -1359,7 +1360,7 @@ private SqlExpression BindProperty(StructuralTypeReferenceExpression typeReferen
13591360
}
13601361
}
13611362

1362-
private Expression BindComplexProperty(StructuralTypeReferenceExpression typeReference, IComplexProperty complexProperty)
1363+
private Expression BindComplexProperty(StructuralTypeReferenceExpression typeReference, IComplexProperty complexProperty, bool forUpdate = false)
13631364
{
13641365
switch (typeReference)
13651366
{
@@ -1370,7 +1371,7 @@ private Expression BindComplexProperty(StructuralTypeReferenceExpression typeRef
13701371
// TODO: Move all this logic into StructuralTypeProjectionExpression, #31376
13711372
Check.DebugAssert(structuralTypeProjection.IsNullable == shaper.IsNullable, "Nullability mismatch");
13721373

1373-
return structuralTypeProjection.BindComplexProperty(complexProperty) switch
1374+
return structuralTypeProjection.BindComplexProperty(complexProperty, forUpdate: forUpdate) switch
13741375
{
13751376
StructuralTypeShaperExpression s => new StructuralTypeReferenceExpression(s),
13761377
CollectionResultExpression c => c,

src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,14 +2855,15 @@ static TableExpressionBase FindRootTableExpressionForColumn(SelectExpression sel
28552855
[EntityFrameworkInternal]
28562856
public static Expression GenerateComplexPropertyShaperExpression(
28572857
StructuralTypeProjectionExpression containerProjection,
2858-
IComplexProperty complexProperty)
2858+
IComplexProperty complexProperty,
2859+
bool forUpdate = false)
28592860
{
28602861
var complexType = complexProperty.ComplexType;
28612862
var propertyExpressionMap = new Dictionary<IProperty, ColumnExpression>();
28622863

28632864
// We do not support complex type splitting, so we will only ever have a single table/view mapping to it.
28642865
// See Issue #32853 and Issue #31248
2865-
var complexTypeTable = complexType.GetViewOrTableMappings().Single().Table;
2866+
ITableBase complexTypeTable = forUpdate ? complexType.GetTableMappings().Single().Table : complexType.GetViewOrTableMappings().Single().Table;
28662867

28672868
if (!containerProjection.TableMap.TryGetValue(complexTypeTable, out var tableAlias))
28682869
{

src/EFCore.Relational/Query/StructuralTypeProjectionExpression.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,14 @@ public virtual ColumnExpression BindProperty(IProperty property)
369369
/// Binds a complex property with this structural type projection to get a shaper expression for the target complex type.
370370
/// </summary>
371371
/// <param name="complexProperty">A complex property to bind.</param>
372+
/// <param name="forUpdate">Is the mapping for read (false) or update (true).</param>
372373
/// <returns>A shaper expression for the target complex type.</returns>
373-
public virtual Expression BindComplexProperty(IComplexProperty complexProperty)
374+
public virtual Expression BindComplexProperty(IComplexProperty complexProperty, bool forUpdate = false)
374375
{
376+
if (forUpdate)
377+
{
378+
return SelectExpression.GenerateComplexPropertyShaperExpression(this, complexProperty, forUpdate: true);
379+
}
375380
if (_complexPropertyCache is null || !_complexPropertyCache.TryGetValue(complexProperty, out var resultShaper))
376381
{
377382
_complexPropertyCache ??= new Dictionary<IComplexProperty, Expression>();

0 commit comments

Comments
 (0)