Skip to content

Commit

Permalink
Allow specify property value without lambda in ExecuteUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Sep 13, 2022
1 parent 6e56123 commit df3986e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,16 @@ when methodCallExpression.Method.IsGenericMethod
&& methodCallExpression.Method.DeclaringType!.IsGenericType
&& methodCallExpression.Method.DeclaringType.GetGenericTypeDefinition() == typeof(SetPropertyCalls<>):

list.Add(
(methodCallExpression.Arguments[0].UnwrapLambdaFromQuote(),
methodCallExpression.Arguments[1].UnwrapLambdaFromQuote()));
var propertyLambda = methodCallExpression.Arguments[0].UnwrapLambdaFromQuote();

list.Add((
propertyLambda,
methodCallExpression.Arguments[1] is UnaryExpression { NodeType: ExpressionType.Quote } quoteExpression
? (LambdaExpression)quoteExpression.Operand
: Expression.Lambda(
methodCallExpression.Arguments[1],
Expression.Parameter(propertyLambda.Parameters[0].Type, propertyLambda.Parameters[0].Name))));

PopulateSetPropertyCalls(methodCallExpression.Object!, list, parameter);

break;
Expand Down
18 changes: 17 additions & 1 deletion src/EFCore.Relational/Query/SetPropertyCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,29 @@ private SetPropertyCalls()
/// <param name="valueExpression">A value expression.</param>
/// <returns>
/// The same instance so that multiple calls to
/// <see cref="SetProperty{TProperty}(Expression{Func{TSource, TProperty}}, Expression{Func{TSource, TProperty}})" /> can be chained.
/// <see cref="SetPropertyCalls{TSource}.SetProperty{TProperty}(Expression{Func{TSource, TProperty}}, Expression{Func{TSource, TProperty}})" />
/// can be chained.
/// </returns>
public SetPropertyCalls<TSource> SetProperty<TProperty>(
Expression<Func<TSource, TProperty>> propertyExpression,
Expression<Func<TSource, TProperty>> valueExpression)
=> throw new InvalidOperationException(RelationalStrings.SetPropertyMethodInvoked);

/// <summary>
/// Specifies a property and corresponding value it should be updated to in ExecuteUpdate method.
/// </summary>
/// <typeparam name="TProperty">The type of property.</typeparam>
/// <param name="propertyExpression">A property access expression.</param>
/// <param name="valueExpression">A value expression.</param>
/// <returns>
/// The same instance so that multiple calls to
/// <see cref="SetPropertyCalls{TSource}.SetProperty{TProperty}(Expression{Func{TSource, TProperty}}, TProperty)" /> can be chained.
/// </returns>
public SetPropertyCalls<TSource> SetProperty<TProperty>(
Expression<Func<TSource, TProperty>> propertyExpression,
TProperty valueExpression)
=> throw new InvalidOperationException(RelationalStrings.SetPropertyMethodInvoked);

#region Hidden System.Object members

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public virtual Task Update_where_hierarchy(bool async)
async,
ss => ss.Set<Animal>().Where(e => e.Name == "Great spotted kiwi"),
e => e,
s => s.SetProperty(e => e.Name, e => "Animal"),
s => s.SetProperty(e => e.Name, "Animal"),
rowsAffectedCount: 1,
(b, a) => a.ForEach(e => Assert.Equal("Animal", e.Name)));

Expand All @@ -113,7 +113,7 @@ public virtual Task Update_where_hierarchy_subquery(bool async)
async,
ss => ss.Set<Animal>().Where(e => e.Name == "Great spotted kiwi").OrderBy(e => e.Name).Skip(0).Take(3),
e => e,
s => s.SetProperty(e => e.Name, e => "Animal"),
s => s.SetProperty(e => e.Name, "Animal"),
rowsAffectedCount: 1);

[ConditionalTheory]
Expand All @@ -123,7 +123,7 @@ public virtual Task Update_where_hierarchy_derived(bool async)
async,
ss => ss.Set<Kiwi>().Where(e => e.Name == "Great spotted kiwi"),
e => e,
s => s.SetProperty(e => e.Name, e => "Kiwi"),
s => s.SetProperty(e => e.Name, "Kiwi"),
rowsAffectedCount: 1);

[ConditionalTheory]
Expand All @@ -133,7 +133,7 @@ public virtual Task Update_where_using_hierarchy(bool async)
async,
ss => ss.Set<Country>().Where(e => e.Animals.Where(a => a.CountryId > 0).Count() > 0),
e => e,
s => s.SetProperty(e => e.Name, e => "Monovia"),
s => s.SetProperty(e => e.Name, "Monovia"),
rowsAffectedCount: 1);

[ConditionalTheory]
Expand All @@ -143,7 +143,7 @@ public virtual Task Update_where_using_hierarchy_derived(bool async)
async,
ss => ss.Set<Country>().Where(e => e.Animals.OfType<Kiwi>().Where(a => a.CountryId > 0).Count() > 0),
e => e,
s => s.SetProperty(e => e.Name, e => "Monovia"),
s => s.SetProperty(e => e.Name, "Monovia"),
rowsAffectedCount: 1);

[ConditionalTheory]
Expand All @@ -155,7 +155,7 @@ public virtual Task Update_where_keyless_entity_mapped_to_sql_query(bool async)
async,
ss => ss.Set<EagleQuery>().Where(e => e.CountryId > 0),
e => e,
s => s.SetProperty(e => e.Name, e => "Eagle"),
s => s.SetProperty(e => e.Name, "Eagle"),
rowsAffectedCount: 1));

protected abstract void ClearLog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public virtual Task Update_where_hierarchy(bool async)
async,
ss => ss.Set<Animal>().Where(e => e.Name == "Great spotted kiwi"),
e => e,
s => s.SetProperty(e => e.Name, e => "Animal"),
s => s.SetProperty(e => e.Name, "Animal"),
rowsAffectedCount: 1,
(b, a) => a.ForEach(e => Assert.Equal("Animal", e.Name)));

Expand All @@ -113,7 +113,7 @@ public virtual Task Update_where_hierarchy_subquery(bool async)
async,
ss => ss.Set<Animal>().Where(e => e.Name == "Great spotted kiwi").OrderBy(e => e.Name).Skip(0).Take(3),
e => e,
s => s.SetProperty(e => e.Name, e => "Animal"),
s => s.SetProperty(e => e.Name, "Animal"),
rowsAffectedCount: 1);

[ConditionalTheory]
Expand All @@ -123,7 +123,7 @@ public virtual Task Update_where_hierarchy_derived(bool async)
async,
ss => ss.Set<Kiwi>().Where(e => e.Name == "Great spotted kiwi"),
e => e,
s => s.SetProperty(e => e.Name, e => "Kiwi"),
s => s.SetProperty(e => e.Name, "Kiwi"),
rowsAffectedCount: 1);

[ConditionalTheory]
Expand All @@ -133,7 +133,7 @@ public virtual Task Update_where_using_hierarchy(bool async)
async,
ss => ss.Set<Country>().Where(e => e.Animals.Where(a => a.CountryId > 0).Count() > 0),
e => e,
s => s.SetProperty(e => e.Name, e => "Monovia"),
s => s.SetProperty(e => e.Name, "Monovia"),
rowsAffectedCount: 2);

[ConditionalTheory]
Expand All @@ -143,7 +143,7 @@ public virtual Task Update_where_using_hierarchy_derived(bool async)
async,
ss => ss.Set<Country>().Where(e => e.Animals.OfType<Kiwi>().Where(a => a.CountryId > 0).Count() > 0),
e => e,
s => s.SetProperty(e => e.Name, e => "Monovia"),
s => s.SetProperty(e => e.Name, "Monovia"),
rowsAffectedCount: 1);

[ConditionalTheory]
Expand All @@ -155,7 +155,7 @@ public virtual Task Update_where_keyless_entity_mapped_to_sql_query(bool async)
async,
ss => ss.Set<EagleQuery>().Where(e => e.CountryId > 0),
e => e,
s => s.SetProperty(e => e.Name, e => "Eagle"),
s => s.SetProperty(e => e.Name, "Eagle"),
rowsAffectedCount: 1));

protected abstract void ClearLog();
Expand Down
Loading

0 comments on commit df3986e

Please sign in to comment.