Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[automated] Merge branch 'release/8.0' => 'main'#31641

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Query.Internal;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public readonly struct QueryableJsonProjectionInfo
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public QueryableJsonProjectionInfo(
Dictionary<IProperty, int> propertyIndexMap,
List<(JsonProjectionInfo, INavigation)> childrenProjectionInfo)
{
PropertyIndexMap = propertyIndexMap;
ChildrenProjectionInfo = childrenProjectionInfo;
}

/// <summary>
/// Map between entity properties and corresponding column indexes.
/// </summary>
/// <remarks>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </remarks>
public IDictionary<IProperty, int> PropertyIndexMap { get; }

/// <summary>
/// Information needed to construct each child JSON entity.
/// - JsonProjection info (same one we use for simple JSON projection),
/// - navigation between parent and the child JSON entity.
/// </summary>
/// <remarks>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </remarks>
public IList<(JsonProjectionInfo JsonProjectionInfo, INavigation Navigation)> ChildrenProjectionInfo { get; }
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -433,7 +433,11 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)

if (newExpression.Arguments[0] is ProjectionBindingExpression projectionBindingExpression)
{
var propertyMap = (IDictionary<IProperty, int>)GetProjectionIndex(projectionBindingExpression);
var projectionIndex = GetProjectionIndex(projectionBindingExpression);
var propertyMap = projectionIndex is IDictionary<IProperty, int>
? (IDictionary<IProperty, int>)projectionIndex
: ((QueryableJsonProjectionInfo)projectionIndex).PropertyIndexMap;

_materializationContextBindings[parameterExpression] = propertyMap;
_entityTypeIdentifyingExpressionInfo[parameterExpression] =
// If single entity type is being selected in hierarchy then we use the value directly else we store the offset
Expand DownExpand Up@@ -535,6 +539,50 @@ protected override Expression VisitExtension(Expression extensionExpression)
visitedShaperResultParameter,
shaper.Type);
}
else if (GetProjectionIndex(projectionBindingExpression) is QueryableJsonProjectionInfo queryableJsonEntityProjectionInfo)
{
if (_isTracking)
{
throw new InvalidOperationException(
RelationalStrings.JsonEntityOrCollectionProjectedAtRootLevelInTrackingQuery(nameof(EntityFrameworkQueryableExtensions.AsNoTracking)));
}

// json entity converted to query root and projected
var entityParameter = Parameter(shaper.Type);
_variables.Add(entityParameter);
var entityMaterializationExpression = (BlockExpression)_parentVisitor.InjectEntityMaterializers(shaper);

var mappedProperties = queryableJsonEntityProjectionInfo.PropertyIndexMap.Keys.ToList();
var rewrittenEntityMaterializationExpression = new QueryableJsonEntityMaterializerRewriter(mappedProperties)
.Rewrite(entityMaterializationExpression);

var visitedEntityMaterializationExpression = Visit(rewrittenEntityMaterializationExpression);
_expressions.Add(Assign(entityParameter, visitedEntityMaterializationExpression));

foreach (var childProjectionInfo in queryableJsonEntityProjectionInfo.ChildrenProjectionInfo)
{
var (jsonReaderDataVariable, keyValuesParameter) = JsonShapingPreProcess(
childProjectionInfo.JsonProjectionInfo,
childProjectionInfo.Navigation.TargetEntityType,
childProjectionInfo.Navigation.IsCollection);

var shaperResult = CreateJsonShapers(
childProjectionInfo.Navigation.TargetEntityType,
nullable: true,
jsonReaderDataVariable,
keyValuesParameter,
parentEntityExpression: entityParameter,
navigation: childProjectionInfo.Navigation);

var visitedShaperResult = Visit(shaperResult);

_includeExpressions.Add(visitedShaperResult);
}

accessor = CompensateForCollectionMaterialization(
entityParameter,
shaper.Type);
}
else
{
var entityParameter = Parameter(shaper.Type);
Expand DownExpand Up@@ -2141,6 +2189,62 @@ ParameterExpression ExtractAndCacheNonConstantJsonArrayElementAccessValue(int in
}
}

private sealed class QueryableJsonEntityMaterializerRewriter : ExpressionVisitor
{
private readonly List<IProperty> _mappedProperties;

public QueryableJsonEntityMaterializerRewriter(List<IProperty> mappedProperties)
{
_mappedProperties = mappedProperties;
}

public BlockExpression Rewrite(BlockExpression jsonEntityShaperMaterializer)
=> (BlockExpression)VisitBlock(jsonEntityShaperMaterializer);

protected override Expression VisitBinary(BinaryExpression binaryExpression)
{
// here we try to pattern match part of the shaper code that checks if key values are null
// if they are all non-null then we generate the entity
// problem for JSON entities is that some of the keys are synthesized and should be omitted
// if the key is one of the mapped ones, we leave the expression as is, otherwise replace with Constant(true)
// i.e. removing it
if (binaryExpression is
{
NodeType: ExpressionType.NotEqual,
Left: MethodCallExpression
{
Method: { IsGenericMethod: true } method,
Arguments: [_, _, ConstantExpression { Value: IProperty property }]
},
Right: ConstantExpression { Value: null }
}
&& method.GetGenericMethodDefinition() == Infrastructure.ExpressionExtensions.ValueBufferTryReadValueMethod)
{
return _mappedProperties.Contains(property)
? binaryExpression
: Constant(true);
}

return base.VisitBinary(binaryExpression);
}

protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression)
{
if (methodCallExpression is
{
Method: { IsGenericMethod: true } method,
Arguments: [_, _, ConstantExpression { Value: IProperty property }]
}
&& method.GetGenericMethodDefinition() == Infrastructure.ExpressionExtensions.ValueBufferTryReadValueMethod
&& !_mappedProperties.Contains(property))
{
return Default(methodCallExpression.Type);
}

return base.VisitMethodCall(methodCallExpression);
}
}

private static LambdaExpression GenerateFixup(
Type entityType,
Type relatedEntityType,
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp