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

Allow generating EF.Property over struct #31753

Merged
merged 1 commit into from
Sep 15, 2023
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
6 changes: 3 additions & 3 deletions src/EFCore/EF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static MethodInfo MakePropertyMethod(Type type)
public static bool IsDesignTime { get; set; }

/// <summary>
/// References a given property or navigation on an entity instance. This is useful for shadow state properties, for
/// References a given property or navigation on an entity or complex type instance. This is useful for shadow state properties, for
/// which no CLR property exists. Currently this method can only be used in LINQ queries and can not be used to
/// access the value assigned to a property in other scenarios.
/// </summary>
Expand All @@ -54,11 +54,11 @@ internal static MethodInfo MakePropertyMethod(Type type)
/// </para>
/// </remarks>
/// <typeparam name="TProperty">The type of the property being referenced.</typeparam>
/// <param name="entity">The entity to access the property on.</param>
/// <param name="instance">The entity or complex type to access the property on.</param>
/// <param name="propertyName">The name of the property.</param>
/// <returns>The value assigned to the property.</returns>
public static TProperty Property<TProperty>(
object entity,
object instance,
[NotParameterized] string propertyName)
=> throw new InvalidOperationException(CoreStrings.PropertyMethodInvoked);

Expand Down
7 changes: 7 additions & 0 deletions src/EFCore/Infrastructure/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,13 @@ private static Expression CreateEFPropertyExpression(
propertyType = propertyType.MakeNullable();
}

// EF.Property expects an object as its first argument. If the target is a struct (complex type), we need an explicit up-cast to
// object.
if (target.Type.IsValueType)
{
target = Expression.Convert(target, typeof(object));
}

return Expression.Call(
EF.MakePropertyMethod(propertyType),
target,
Expand Down
Loading