internal override ObjectSpanRewriter.SpanTrackingInfo CreateEntitySpanTrackingInfo(
     DbExpression expression,
     EntityType entityType)
 {
     ObjectSpanRewriter.SpanTrackingInfo spanTrackingInfo = new ObjectSpanRewriter.SpanTrackingInfo();
     ObjectFullSpanRewriter.SpanPathInfo spanPathInfo     = this._currentSpanPath.Peek();
     if (spanPathInfo.Children != null)
     {
         int index = 1;
         foreach (KeyValuePair <NavigationProperty, ObjectFullSpanRewriter.SpanPathInfo> child in spanPathInfo.Children)
         {
             if (spanTrackingInfo.ColumnDefinitions == null)
             {
                 spanTrackingInfo = this.InitializeTrackingInfo(this.RelationshipSpan);
             }
             DbExpression expression1 = (DbExpression)expression.Property(child.Key);
             this._currentSpanPath.Push(child.Value);
             DbExpression dbExpression = this.Rewrite(expression1);
             this._currentSpanPath.Pop();
             spanTrackingInfo.ColumnDefinitions.Add(new KeyValuePair <string, DbExpression>(spanTrackingInfo.ColumnNames.Next(), dbExpression));
             AssociationEndMember propertyTargetEnd = this.GetNavigationPropertyTargetEnd(child.Key);
             spanTrackingInfo.SpannedColumns[index] = propertyTargetEnd;
             if (this.RelationshipSpan)
             {
                 spanTrackingInfo.FullSpannedEnds[propertyTargetEnd] = true;
             }
             ++index;
         }
     }
     return(spanTrackingInfo);
 }
 internal ObjectSpanRewriter.SpanTrackingInfo InitializeTrackingInfo(
     bool createAssociationEndTrackingInfo)
 {
     ObjectSpanRewriter.SpanTrackingInfo spanTrackingInfo = new ObjectSpanRewriter.SpanTrackingInfo();
     spanTrackingInfo.ColumnDefinitions = new List <KeyValuePair <string, DbExpression> >();
     spanTrackingInfo.ColumnNames       = new AliasGenerator(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Span{0}_Column", (object)this._spanCount));
     spanTrackingInfo.SpannedColumns    = new Dictionary <int, AssociationEndMember>();
     if (createAssociationEndTrackingInfo)
     {
         spanTrackingInfo.FullSpannedEnds = new Dictionary <AssociationEndMember, bool>();
     }
     return(spanTrackingInfo);
 }
        private DbExpression RewriteEntity(DbExpression expression, EntityType entityType)
        {
            if (DbExpressionKind.NewInstance == expression.ExpressionKind)
            {
                return(expression);
            }
            ++this._spanCount;
            int spanCount = this._spanCount;

            ObjectSpanRewriter.SpanTrackingInfo spanTrackingInfo = this.CreateEntitySpanTrackingInfo(expression, entityType);
            List <KeyValuePair <AssociationEndMember, AssociationEndMember> > relationshipSpanEnds = this.GetRelationshipSpanEnds(entityType);

            if (relationshipSpanEnds != null)
            {
                if (spanTrackingInfo.ColumnDefinitions == null)
                {
                    spanTrackingInfo = this.InitializeTrackingInfo(false);
                }
                int index = spanTrackingInfo.ColumnDefinitions.Count + 1;
                foreach (KeyValuePair <AssociationEndMember, AssociationEndMember> keyValuePair in relationshipSpanEnds)
                {
                    if (spanTrackingInfo.FullSpannedEnds == null || !spanTrackingInfo.FullSpannedEnds.ContainsKey(keyValuePair.Value))
                    {
                        DbExpression source = (DbExpression)null;
                        if (!this.TryGetNavigationSource(keyValuePair.Value, out source))
                        {
                            source = (DbExpression)expression.GetEntityRef().NavigateAllowingAllRelationshipsInSameTypeHierarchy((RelationshipEndMember)keyValuePair.Key, (RelationshipEndMember)keyValuePair.Value);
                        }
                        spanTrackingInfo.ColumnDefinitions.Add(new KeyValuePair <string, DbExpression>(spanTrackingInfo.ColumnNames.Next(), source));
                        spanTrackingInfo.SpannedColumns[index] = keyValuePair.Value;
                        ++index;
                    }
                }
            }
            if (spanTrackingInfo.ColumnDefinitions == null)
            {
                --this._spanCount;
                return(expression);
            }
            spanTrackingInfo.ColumnDefinitions.Insert(0, new KeyValuePair <string, DbExpression>(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Span{0}_SpanRoot", (object)spanCount), expression));
            DbExpression dbExpression = (DbExpression)DbExpressionBuilder.NewRow((IEnumerable <KeyValuePair <string, DbExpression> >)spanTrackingInfo.ColumnDefinitions);

            this.AddSpanMap((RowType)dbExpression.ResultType.EdmType, spanTrackingInfo.SpannedColumns);
            return(dbExpression);
        }