Пример #1
0
        private void AddDiscriminator(SelectExpression selectExpression, IEntityType entityType)
        {
            var concreteEntityTypes = entityType.GetConcreteDerivedTypesInclusive().ToList();

            if (concreteEntityTypes.Count == 1)
            {
                var concreteEntityType = concreteEntityTypes[0];
                if (concreteEntityType.GetDiscriminatorProperty() != null)
                {
                    var discriminatorColumn = ((EntityProjectionExpression)selectExpression.GetMappedProjection(new ProjectionMember()))
                                              .BindProperty(concreteEntityType.GetDiscriminatorProperty(), clientEval: false);

                    selectExpression.ApplyPredicate(
                        Equal((SqlExpression)discriminatorColumn, Constant(concreteEntityType.GetDiscriminatorValue())));
                }
            }
            else
            {
                var discriminatorColumn = ((EntityProjectionExpression)selectExpression.GetMappedProjection(new ProjectionMember()))
                                          .BindProperty(concreteEntityTypes[0].GetDiscriminatorProperty(), clientEval: false);

                selectExpression.ApplyPredicate(
                    In(
                        (SqlExpression)discriminatorColumn, Constant(concreteEntityTypes.Select(et => et.GetDiscriminatorValue()).ToList()),
                        negated: false));
            }
        }
Пример #2
0
        /// <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>
        protected override Expression VisitExtension(Expression extensionExpression)
        {
            Check.NotNull(extensionExpression, nameof(extensionExpression));

            switch (extensionExpression)
            {
            case EntityShaperExpression entityShaperExpression:
            {
                var projectionBindingExpression = (ProjectionBindingExpression)entityShaperExpression.ValueBufferExpression;
                VerifySelectExpression(projectionBindingExpression);

                if (_clientEval)
                {
                    var entityProjection = (EntityProjectionExpression)_selectExpression.GetMappedProjection(
                        projectionBindingExpression.ProjectionMember);

                    return(entityShaperExpression.Update(
                               new ProjectionBindingExpression(
                                   _selectExpression, _selectExpression.AddToProjection(entityProjection), typeof(ValueBuffer))));
                }

                _projectionMapping[_projectionMembers.Peek()]
                    = _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember);

                return(entityShaperExpression.Update(
                           new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), typeof(ValueBuffer))));
            }

            case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:
                return(materializeCollectionNavigationExpression.Navigation is INavigation embeddableNavigation &&
                       embeddableNavigation.IsEmbedded()
                            ? base.Visit(materializeCollectionNavigationExpression.Subquery)
                            : base.VisitExtension(materializeCollectionNavigationExpression));

            case IncludeExpression includeExpression:
                if (!_clientEval)
                {
                    return(null);
                }

                if (!(includeExpression.Navigation is INavigation includableNavigation &&
                      includableNavigation.IsEmbedded()))
                {
                    throw new InvalidOperationException(
                              CosmosStrings.NonEmbeddedIncludeNotSupported(includeExpression.Navigation));
                }

                _includedNavigations.Push(includableNavigation);

                var newIncludeExpression = base.VisitExtension(includeExpression);

                _includedNavigations.Pop();

                return(newIncludeExpression);

            default:
                throw new InvalidOperationException(CoreStrings.TranslationFailed(extensionExpression.Print()));
            }
        }
        /// <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>
        protected override Expression VisitExtension(Expression extensionExpression)
        {
            switch (extensionExpression)
            {
            case EntityShaperExpression entityShaperExpression:
            {
                var projectionBindingExpression = (ProjectionBindingExpression)entityShaperExpression.ValueBufferExpression;
                VerifySelectExpression(projectionBindingExpression);

                if (_clientEval)
                {
                    var entityProjection = (EntityProjectionExpression)_selectExpression.GetMappedProjection(
                        projectionBindingExpression.ProjectionMember);

                    return(entityShaperExpression.Update(
                               new ProjectionBindingExpression(
                                   _selectExpression, _selectExpression.AddToProjection(entityProjection), typeof(ValueBuffer))));
                }

                _projectionMapping[_projectionMembers.Peek()]
                    = _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember);

                return(entityShaperExpression.Update(
                           new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), typeof(ValueBuffer))));
            }

            case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:
                if (materializeCollectionNavigationExpression.Navigation.IsEmbedded())
                {
                    var subquery = materializeCollectionNavigationExpression.Subquery;
                    // Unwrap AsQueryable around the subquery if present
                    if (subquery is MethodCallExpression innerMethodCall &&
                        innerMethodCall.Method.IsGenericMethod &&
                        innerMethodCall.Method.GetGenericMethodDefinition() == QueryableMethodProvider.AsQueryableMethodInfo)
                    {
                        subquery = innerMethodCall.Arguments[0];
                    }

                    return(base.Visit(subquery));
                }
                else
                {
                    return(base.VisitExtension(materializeCollectionNavigationExpression));
                }

            case IncludeExpression includeExpression:
                return(_clientEval ? base.VisitExtension(includeExpression) : null);

            default:
                throw new InvalidOperationException(new ExpressionPrinter().Print(extensionExpression));
            }
        }
        /// <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>
        protected override Expression VisitExtension(Expression extensionExpression)
        {
            switch (extensionExpression)
            {
            case EntityShaperExpression entityShaperExpression:
            {
                var projectionBindingExpression = (ProjectionBindingExpression)entityShaperExpression.ValueBufferExpression;
                VerifySelectExpression(projectionBindingExpression);

                if (_clientEval)
                {
                    var entityProjection = (EntityProjectionExpression)_selectExpression.GetMappedProjection(
                        projectionBindingExpression.ProjectionMember);

                    return(entityShaperExpression.Update(
                               new ProjectionBindingExpression(
                                   _selectExpression, _selectExpression.AddToProjection(entityProjection), typeof(ValueBuffer))));
                }

                _projectionMapping[_projectionMembers.Peek()]
                    = _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember);

                return(entityShaperExpression.Update(
                           new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), typeof(ValueBuffer))));
            }

            case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:
                return(materializeCollectionNavigationExpression.Navigation.IsEmbedded()
                        ? base.Visit(materializeCollectionNavigationExpression.Subquery)
                        : base.VisitExtension(materializeCollectionNavigationExpression));

            case IncludeExpression includeExpression:
                return(_clientEval ? base.VisitExtension(includeExpression) : null);

            default:
                throw new InvalidOperationException(new ExpressionPrinter().Print(extensionExpression));
            }
        }
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? (int)((ConstantExpression)_selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)).Value
         : projectionBindingExpression.Index
 ?? throw new InvalidOperationException(CoreStrings.QueryFailed(projectionBindingExpression.Print(), GetType().Name));
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue <int>()
         : (projectionBindingExpression.Index
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? (int)((ConstantExpression)_selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)).Value
         : projectionBindingExpression.Index ?? throw new InvalidOperationException();
Пример #8
0
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue <int>()
         : projectionBindingExpression.Index
 ?? throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print()));