private NewExpression CreateEntityInfoExpression(IEntityType entityType, INavigation navigation)
        {
            var valueBufferFactory = ValueBufferFactoryFactory.Create(entityType);

            var materializationContextParameter
                = Expression.Parameter(typeof(MaterializationContext), "materializationContext");
            var materializer = Expression.Lambda(_entityMaterializerSource
                                                 .CreateMaterializeExpression(
                                                     entityType, materializationContextParameter), materializationContextParameter);

            var nestedEntities = new List <Expression>();

            foreach (var ownedNavigation in entityType.GetNavigations())
            {
                var fk = ownedNavigation.ForeignKey;
                if (!fk.IsOwnership ||
                    ownedNavigation.IsDependentToPrincipal() ||
                    fk.DeclaringEntityType.IsDocumentRoot())
                {
                    continue;
                }

                nestedEntities.Add(CreateEntityInfoExpression(fk.DeclaringEntityType, ownedNavigation));
            }

            var nestedEntitiesExpression = nestedEntities.Count == 0
                ? (Expression)Expression.Constant(null, typeof(IList <EntityInfo>))
                : Expression.ListInit(Expression.New(typeof(List <EntityInfo>)),
                                      nestedEntities.Select(n => Expression.ElementInit(_listAddMethodInfo, n)));

            return(Expression.New(
                       EntityInfo.ConstructorInfo,
                       Expression.Constant(navigation, typeof(INavigation)),
                       Expression.Constant(entityType.FindPrimaryKey()),
                       valueBufferFactory,
                       materializer,
                       nestedEntitiesExpression));
        }
Beispiel #2
0
        private NewExpression CreateEntityInfoExpression(IEntityType entityType, INavigation navigation)
        {
            var usedProperties = new List <IProperty>();
            var materializer   = CreateMaterializerExpression(entityType, usedProperties, out var indexMap);

            var valueBufferFactory = ValueBufferFactoryFactory.Create(usedProperties);

            var nestedEntities = new List <Expression>();

            foreach (var ownedNavigation in entityType.GetNavigations().Concat(entityType.GetDerivedNavigations()))
            {
                var fk = ownedNavigation.ForeignKey;
                if (!fk.IsOwnership ||
                    ownedNavigation.IsDependentToPrincipal() ||
                    fk.DeclaringEntityType.IsDocumentRoot())
                {
                    continue;
                }

                nestedEntities.Add(CreateEntityInfoExpression(fk.DeclaringEntityType, ownedNavigation));
            }

            var nestedEntitiesExpression = nestedEntities.Count == 0
                ? (Expression)Expression.Constant(null, typeof(IList <EntityInfo>))
                : Expression.ListInit(
                Expression.New(typeof(List <EntityInfo>)),
                nestedEntities.Select(n => Expression.ElementInit(_listAddMethodInfo, n)));

            return(Expression.New(
                       EntityInfo.ConstructorInfo,
                       Expression.Constant(navigation, typeof(INavigation)),
                       Expression.Constant(entityType.FindPrimaryKey(), typeof(IKey)),
                       valueBufferFactory,
                       materializer,
                       Expression.Constant(indexMap, typeof(Dictionary <Type, int[]>)),
                       nestedEntitiesExpression));
        }