Esempio n. 1
0
        private static EdmNavigationProperty CreateNavigationProperty(FKeyInfo fkeyInfo)
        {
            EdmEntityType edmDependent = fkeyInfo.DependentInfo.EdmType;
            EdmEntityType edmPrincipal = fkeyInfo.PrincipalInfo.EdmType;

            EdmStructuralProperty[] dependentEdmProperties = CreateDependentEdmProperties(edmDependent, fkeyInfo.DependentStructuralProperties);
            var edmDependentInfo = new EdmNavigationPropertyInfo()
            {
                ContainsTarget      = false,
                Name                = fkeyInfo.DependentNavigationProperty.Name,
                DependentProperties = dependentEdmProperties,
                OnDelete            = EdmOnDeleteAction.None,
                PrincipalProperties = edmPrincipal.DeclaredKey,
                Target              = edmPrincipal,
                TargetMultiplicity  = fkeyInfo.DependentMultiplicity
            };

            if (fkeyInfo.PrincipalNavigationProperty == null || fkeyInfo.PrincipalNavigationProperty == fkeyInfo.DependentNavigationProperty)
            {
                return(edmDependent.AddUnidirectionalNavigation(edmDependentInfo));
            }

            var edmPrincipalInfo = new EdmNavigationPropertyInfo()
            {
                ContainsTarget      = false,
                Name                = fkeyInfo.PrincipalNavigationProperty.Name,
                DependentProperties = null,
                OnDelete            = EdmOnDeleteAction.None,
                PrincipalProperties = edmPrincipal.DeclaredKey,
                Target              = edmDependent,
                TargetMultiplicity  = fkeyInfo.PrincipalMultiplicity
            };

            return(edmDependent.AddBidirectionalNavigation(edmDependentInfo, edmPrincipalInfo));
        }
        public EdmDeltaModel(IEdmModel source, IEdmEntityType entityType, IEnumerable <string> propertyNames)
        {
            _source     = source;
            _entityType = new EdmEntityType(entityType.Namespace, entityType.Name);

            foreach (var property in entityType.StructuralProperties())
            {
                if (propertyNames.Contains(property.Name))
                {
                    _entityType.AddStructuralProperty(property.Name, property.Type, property.DefaultValueString, property.ConcurrencyMode);
                }
            }

            foreach (var property in entityType.NavigationProperties())
            {
                if (propertyNames.Contains(property.Name))
                {
                    var navInfo = new EdmNavigationPropertyInfo()
                    {
                        ContainsTarget      = property.ContainsTarget,
                        DependentProperties = property.DependentProperties(),
                        Name     = property.Name,
                        OnDelete = property.OnDelete,
                        Target   = property.Partner != null
                            ? property.Partner.DeclaringEntityType()
                            : property.Type.TypeKind() == EdmTypeKind.Collection
                            ? (property.Type.Definition as IEdmCollectionType).ElementType.Definition as IEdmEntityType
                            : null,
                        TargetMultiplicity = property.TargetMultiplicity(),
                    };
                    _entityType.AddUnidirectionalNavigation(navInfo);
                }
            }
        }
Esempio n. 3
0
        public void TestCloneEdmnavigationPropertyInfo()
        {
            var type      = new EdmEntityType("NS", "name");
            var property1 = new EdmStructuralProperty(type, "property1", EdmCoreModel.Instance.GetInt32(false));
            var property2 = new EdmStructuralProperty(type, "property2", EdmCoreModel.Instance.GetInt32(false));
            EdmNavigationPropertyInfo navigationPropertyInfo = new EdmNavigationPropertyInfo
            {
                ContainsTarget      = true,
                DependentProperties = new[] { property1 },
                Name                = "navPropInfo",
                OnDelete            = EdmOnDeleteAction.Cascade,
                PrincipalProperties = new[] { property2 },
                Target              = type,
                TargetMultiplicity  = EdmMultiplicity.Many
            };
            var cloneNavigationPropertyInfo = navigationPropertyInfo.Clone();

            Assert.Equal(navigationPropertyInfo.ContainsTarget, cloneNavigationPropertyInfo.ContainsTarget);
            Assert.Equal(navigationPropertyInfo.Name, cloneNavigationPropertyInfo.Name);
            Assert.Equal(navigationPropertyInfo.DependentProperties, cloneNavigationPropertyInfo.DependentProperties);
            Assert.Equal(navigationPropertyInfo.OnDelete, cloneNavigationPropertyInfo.OnDelete);
            Assert.Equal(navigationPropertyInfo.Target, cloneNavigationPropertyInfo.Target);
            Assert.Equal(navigationPropertyInfo.PrincipalProperties, cloneNavigationPropertyInfo.PrincipalProperties);
            Assert.Equal(navigationPropertyInfo.TargetMultiplicity, cloneNavigationPropertyInfo.TargetMultiplicity);
        }
Esempio n. 4
0
        private void CreateEntityTypeBody(EdmEntityType type, EntityTypeConfiguration config)
        {
            Contract.Assert(type != null);
            Contract.Assert(config != null);

            CreateStructuralTypeBody(type, config);
            IEnumerable <IEdmStructuralProperty> keys = config.Keys.Select(p => type.DeclaredProperties.OfType <IEdmStructuralProperty>().First(dp => dp.Name == p.Name));

            type.AddKeys(keys);

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo();
                info.Name = navProp.Name;
                info.TargetMultiplicity = navProp.Multiplicity;
                info.Target             = GetEdmType(navProp.RelatedClrType) as IEdmEntityType;
                IEdmProperty edmProperty = type.AddUnidirectionalNavigation(info);
                if (navProp.PropertyInfo != null && edmProperty != null)
                {
                    _properties[navProp.PropertyInfo] = edmProperty;
                }

                if (edmProperty != null && navProp.IsRestricted)
                {
                    _propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
                }
            }
        }
Esempio n. 5
0
        public void BuildNavigationProperty()
        {
            EdmStructuralProperty[] dependentEdmProperties = CreateDependentEdmProperties(DependentInfo.EdmType, DependentStructuralProperties);

            IEdmNavigationProperty    edmNavigationProperty;
            EdmNavigationPropertyInfo edmPrincipalInfo;

            if (DependentNavigationProperty == null)
            {
                if (PrincipalNavigationProperty == null)
                {
                    throw new InvalidOperationException("If not set DependentNavigationProperty must set PrincipalNavigationProperty");
                }

                edmPrincipalInfo = new EdmNavigationPropertyInfo()
                {
                    ContainsTarget      = false,
                    Name                = PrincipalNavigationProperty.Name,
                    DependentProperties = dependentEdmProperties,
                    OnDelete            = EdmOnDeleteAction.None,
                    PrincipalProperties = PrincipalInfo.EdmType.DeclaredKey,
                    Target              = DependentInfo.EdmType,
                    TargetMultiplicity  = PrincipalMultiplicity
                };
                edmNavigationProperty = AddPrincipalNavigation(edmPrincipalInfo, false);
            }
            else
            {
                var edmDependentInfo = new EdmNavigationPropertyInfo()
                {
                    ContainsTarget      = false,
                    Name                = DependentNavigationProperty.Name,
                    DependentProperties = dependentEdmProperties,
                    OnDelete            = EdmOnDeleteAction.None,
                    PrincipalProperties = PrincipalInfo.EdmType.DeclaredKey,
                    Target              = PrincipalInfo.EdmType,
                    TargetMultiplicity  = DependentMultiplicity
                };
                if (PrincipalNavigationProperty == null || PrincipalNavigationProperty == DependentNavigationProperty)
                {
                    edmNavigationProperty = AddDependentNavigation(edmDependentInfo, false);
                }
                else
                {
                    edmPrincipalInfo = new EdmNavigationPropertyInfo()
                    {
                        ContainsTarget      = false,
                        Name                = PrincipalNavigationProperty.Name,
                        DependentProperties = null,
                        OnDelete            = EdmOnDeleteAction.None,
                        PrincipalProperties = PrincipalInfo.EdmType.DeclaredKey,
                        Target              = DependentInfo.EdmType,
                        TargetMultiplicity  = PrincipalMultiplicity
                    };
                    edmNavigationProperty = AddBidirectionalNavigation(edmDependentInfo, edmPrincipalInfo);
                }
            }
            EdmNavigationProperty = edmNavigationProperty;
        }
Esempio n. 6
0
        private static void ReferentialConstraintDemo()
        {
            EdmModel model = new EdmModel();

            var customer = new EdmEntityType("ns", "Customer");

            model.AddElement(customer);
            var customerId = customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false);

            customer.AddKeys(customerId);
            var address = new EdmComplexType("ns", "Address");

            model.AddElement(address);
            var code = address.AddStructuralProperty("gid", EdmPrimitiveTypeKind.Guid);

            customer.AddStructuralProperty("addr", new EdmComplexTypeReference(address, true));

            var order = new EdmEntityType("ns", "Order");

            model.AddElement(order);
            var oId = order.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false);

            order.AddKeys(oId);

            var orderCustomerId = order.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32, false);

            var nav = new EdmNavigationPropertyInfo()
            {
                Name                = "NavCustomer",
                Target              = customer,
                TargetMultiplicity  = EdmMultiplicity.One,
                DependentProperties = new[] { orderCustomerId },
                PrincipalProperties = new[] { customerId }
            };

            order.AddUnidirectionalNavigation(nav);

            IEnumerable <EdmError> errors;

            var writer = XmlWriter.Create(Console.Out, new XmlWriterSettings {
                Indent = true
            });

            EdmxWriter.TryWriteEdmx(model, writer, EdmxTarget.OData, out errors);
            writer.Flush();

            foreach (var edmError in errors)
            {
                Console.WriteLine(edmError);
            }

            model.Validate(out errors);
            foreach (var edmError in errors)
            {
                Console.WriteLine(edmError);
            }
        }
Esempio n. 7
0
        private void CreateNavigationProperty(StructuralTypeConfiguration config, List <EdmNavigationPropertyInfo> infos)
        {
            Contract.Assert(config != null);

            EdmEntityType type = (EdmEntityType)(GetEdmType(config.ClrType));

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                Func <NavigationPropertyConfiguration, EdmNavigationPropertyInfo> getInfo = nav =>
                {
                    EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
                    {
                        Name = nav.Name,
                        TargetMultiplicity = nav.Multiplicity,
                        Target             = GetEdmType(nav.RelatedClrType) as IEdmEntityType,
                        ContainsTarget     = nav.ContainsTarget,
                        OnDelete           = nav.OnDeleteAction
                    };

                    // Principal properties
                    if (nav.PrincipalProperties.Any())
                    {
                        info.PrincipalProperties = GetDeclaringPropertyInfo(nav.PrincipalProperties);
                    }

                    // Dependent properties
                    if (nav.DependentProperties.Any())
                    {
                        info.DependentProperties = GetDeclaringPropertyInfo(nav.DependentProperties);
                    }
                    infos.Add(info);
                    return(info);
                };
                var          navInfo = getInfo(navProp);
                IEdmProperty edmProperty;
                if (navProp.Partner != null)
                {
                    edmProperty = type.AddBidirectionalNavigation(navInfo,
                                                                  getInfo(navProp.Partner)
                                                                  //infos.Single(i => i.Name == navProp.Partner.Name && i.Target == GetEdmType(navProp.Partner.RelatedClrType))
                                                                  );
                }
                else
                {
                    edmProperty = type.AddUnidirectionalNavigation(navInfo);
                }
                if (navProp.PropertyInfo != null && edmProperty != null)
                {
                    _properties[navProp.PropertyInfo] = edmProperty;
                }

                if (edmProperty != null && navProp.IsRestricted)
                {
                    _propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
                }
            }
        }
Esempio n. 8
0
        public ODataNavigationPropertySegmentTests()
        {
            _entityType = new EdmEntityType("NS", "Entity");
            EdmNavigationPropertyInfo propertyInfo = new EdmNavigationPropertyInfo
            {
                Name = "Nav",
                TargetMultiplicity = EdmMultiplicity.One,
                Target             = _entityType
            };

            _property = EdmNavigationProperty.CreateNavigationProperty(_entityType, propertyInfo);
        }
        private static void AddManyToOneNavigation(string navTargetName, EdmEntitySet oneEntitySet, EdmEntitySet manyEntitySet, EdmEntityType oneEntityType, EdmEntityType manyEntityType)
        {
            var navPropertyInfo = new EdmNavigationPropertyInfo
            {
                TargetMultiplicity = EdmMultiplicity.One,
                Target             = oneEntityType,
                ContainsTarget     = false,
                OnDelete           = EdmOnDeleteAction.None,
                Name = navTargetName
            };

            manyEntitySet.AddNavigationTarget(manyEntityType.AddUnidirectionalNavigation(navPropertyInfo), oneEntitySet);
        }
        private IEdmModel GetModel()
        {
            EdmModel myModel = new EdmModel();

            EdmComplexType shippingAddress = new EdmComplexType("MyNS", "ShippingAddress");

            shippingAddress.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            shippingAddress.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            shippingAddress.AddStructuralProperty("Region", EdmPrimitiveTypeKind.String);
            shippingAddress.AddStructuralProperty("PostalCode", EdmPrimitiveTypeKind.String);
            myModel.AddElement(shippingAddress);

            EdmComplexTypeReference shippingAddressReference = new EdmComplexTypeReference(shippingAddress, true);

            EdmEntityType order = new EdmEntityType("MyNS", "Order");

            order.AddStructuralProperty("ShippingAddress", shippingAddressReference);
            myModel.AddElement(order);

            EdmEntityType person = new EdmEntityType("MyNS", "Person");

            myModel.AddElement(person);

            customer = new EdmEntityType("MyNS", "Customer");
            customer.AddStructuralProperty("ContactName", EdmPrimitiveTypeKind.String);
            EdmNavigationPropertyInfo orderLinks = new EdmNavigationPropertyInfo
            {
                Name               = "Orders",
                Target             = order,
                TargetMultiplicity = EdmMultiplicity.Many
            };
            EdmNavigationPropertyInfo personLinks = new EdmNavigationPropertyInfo
            {
                Name               = "Parent",
                Target             = person,
                TargetMultiplicity = EdmMultiplicity.Many
            };

            customer.AddUnidirectionalNavigation(orderLinks);
            customer.AddUnidirectionalNavigation(personLinks);
            myModel.AddElement(customer);

            EdmEntityContainer container = new EdmEntityContainer("MyNS", "Example30");

            customers = container.AddEntitySet("Customers", customer);
            container.AddEntitySet("Orders", order);
            myModel.AddElement(container);

            return(myModel);
        }
        private void CreateNavigationProperty(StructuralTypeConfiguration config)
        {
            Contract.Assert(config != null);

            EdmStructuredType type = (EdmStructuredType)(this.GetEdmType(config.ClrType));

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
                {
                    Name = navProp.Name,
                    TargetMultiplicity = navProp.Multiplicity,
                    Target             = this.GetEdmType(navProp.RelatedClrType) as IEdmEntityType,
                    ContainsTarget     = navProp.ContainsTarget,
                    OnDelete           = navProp.OnDeleteAction
                };

                // Principal properties
                if (navProp.PrincipalProperties.Any())
                {
                    info.PrincipalProperties = this.GetDeclaringPropertyInfo(navProp.PrincipalProperties);
                }

                // Dependent properties
                if (navProp.DependentProperties.Any())
                {
                    info.DependentProperties = this.GetDeclaringPropertyInfo(navProp.DependentProperties);
                }

                IEdmProperty edmProperty = type.AddUnidirectionalNavigation(info);
                if (navProp.PropertyInfo != null && edmProperty != null)
                {
                    this._properties[navProp.PropertyInfo] = edmProperty;
                }

                if (edmProperty != null)
                {
                    if (navProp.IsRestricted)
                    {
                        this._propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
                    }

                    if (navProp.QueryConfiguration.ModelBoundQuerySettings != null)
                    {
                        this._propertiesQuerySettings.Add(edmProperty, navProp.QueryConfiguration.ModelBoundQuerySettings);
                    }
                }
            }
        }
Esempio n. 12
0
        private void AddProperty(EdmStructuredType edmType, RdmProperty rdmProp)
        {
            var         edmTypeRef = env.ResolveTypeReference(rdmProp.Type);
            EdmProperty edmProp;

            // collection navigation property
            if (edmTypeRef is IEdmCollectionTypeReference collRef &&
                collRef.Definition is IEdmCollectionType collType &&
                collType.ElementType is IEdmEntityTypeReference elTypeRef &&
                elTypeRef.Definition is IEdmEntityType elType)
            {
                var info = new EdmNavigationPropertyInfo {
                    Name = rdmProp.Name, Target = elType, TargetMultiplicity = EdmMultiplicity.Many
                };
                edmProp = edmType.AddUnidirectionalNavigation(info);
            }
Esempio n. 13
0
        private void DoIt(NavigationPropertyConfiguration navProp, EdmEntityType type)
        {
            foreach (var dependentProperty in navProp.DependentProperties)
            {
                if (dependentProperty.GetConfiguration(_configurations).IsIgnored)
                {
                    navProp.IsIgnored = true;
                    return;
                }
            }
            if (navProp.IsIgnored)
            {
                return;
            }
            EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
            {
                Name = navProp.Name,
                TargetMultiplicity = navProp.Multiplicity,
                Target             = GetEdmType(navProp.RelatedClrType) as IEdmEntityType,
                ContainsTarget     = navProp.ContainsTarget,
                OnDelete           = navProp.OnDeleteAction
            };

            // Principal properties
            if (navProp.PrincipalProperties.Any())
            {
                info.PrincipalProperties = GetDeclaringPropertyInfo(navProp.PrincipalProperties);
            }

            // Dependent properties
            if (navProp.DependentProperties.Any())
            {
                info.DependentProperties = GetDeclaringPropertyInfo(navProp.DependentProperties);
            }

            IEdmProperty edmProperty = type.AddUnidirectionalNavigation(info);

            if (navProp.PropertyInfo != null && edmProperty != null)
            {
                _properties[navProp.PropertyInfo] = edmProperty;
            }

            if (edmProperty != null && navProp.IsRestricted)
            {
                _propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
            }
        }
Esempio n. 14
0
        private void CreateEntityTypeBody(EdmEntityType type, IEntityTypeConfiguration config)
        {
            CreateStructuralTypeBody(type, config);
            IEdmStructuralProperty[] keys = config.Keys.Select(p => type.DeclaredProperties.OfType <IEdmStructuralProperty>().First(dp => dp.Name == p.PropertyInfo.Name)).ToArray();
            type.AddKeys(keys);

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo();
                info.Name = navProp.Name;
                info.TargetMultiplicity = navProp.Multiplicity;
                info.Target             = _types[navProp.RelatedClrType.FullName] as IEdmEntityType;
                //TODO: If target end has a multiplity of 1 this assumes the source end is 0..1.
                //      I think a better default multiplicity is *
                type.AddUnidirectionalNavigation(info);
            }
        }
Esempio n. 15
0
        private static IEdmNavigationSource GetContainedEntitySet(EdmModel model, EdmEntityType type)
        {
            EdmEntitySet entitySet              = GetEntitySet(model, type);
            var          containedEntity        = GetContainedEntityType(model);
            var          navigationPropertyInfo = new EdmNavigationPropertyInfo()
            {
                Name               = "nav",
                Target             = containedEntity,
                TargetMultiplicity = EdmMultiplicity.Many, // todo: TargetMultiplicity info seems lost in V4
                ContainsTarget     = true
            };
            EdmNavigationProperty navigationProperty = EdmNavigationProperty.CreateNavigationProperty(type, navigationPropertyInfo);

            type.AddUnidirectionalNavigation(navigationPropertyInfo);
            IEdmNavigationSource containedEntitySet = entitySet.FindNavigationTarget(navigationProperty);

            return(containedEntitySet);
        }
        public void KindPropertyReturnsNavigationProperty()
        {
            // Arrange
            EdmNavigationPropertyInfo propertyInfo = new EdmNavigationPropertyInfo
            {
                Name = "Nav",
                TargetMultiplicity = EdmMultiplicity.One,
                Target             = _compositeKeyEntityType
            };
            var property = EdmNavigationProperty.CreateNavigationProperty(_simpleKeyEntityType, propertyInfo);
            ODataNavigationPropertySegment npSegment = new ODataNavigationPropertySegment(property);
            ODataNavigationSourceSegment   nsSegment = new ODataNavigationSourceSegment(_simpleKeyEntitySet);
            ODataKeySegment keySegment = new ODataKeySegment(_simpleKeyEntityType);
            ODataPath       path       = new ODataPath(nsSegment, keySegment, npSegment);

            // Act & Assert
            Assert.Equal(ODataPathKind.NavigationProperty, path.Kind);
        }
Esempio n. 17
0
        public void Build(EntityTypeInfo typeInfo)
        {
            foreach ((PropertyInfo many, PropertyInfo join) in GetManyToManyInfo(_metadataProvider, typeInfo.ClrType))
            {
                if (many == null || many.DeclaringType != typeInfo.ClrType)
                {
                    continue;
                }

                IEdmNavigationProperty joinNavigationProperty = GetJoinNavigationProperty(typeInfo, join.DeclaringType);
                if (joinNavigationProperty == null)
                {
                    continue;
                }

                EntityTypeInfo principalInfo = _entityTypeInfos[join.PropertyType];
                EntityTypeInfo dependentInfo = _entityTypeInfos[many.DeclaringType];

                var edmDependentInfo = new EdmNavigationPropertyInfo()
                {
                    ContainsTarget      = true,
                    Name                = many.Name,
                    OnDelete            = EdmOnDeleteAction.None,
                    PrincipalProperties = principalInfo.EdmType.DeclaredKey,
                    Target              = principalInfo.EdmType,
                    TargetMultiplicity  = EdmMultiplicity.Many
                };
                IEdmNavigationProperty edmManyToManyProperty;
                if (typeInfo.ClrType.GetProperty(many.Name) == null)
                {
                    IEdmNavigationProperty edmNavigationProperty = EdmNavigationProperty.CreateNavigationProperty(dependentInfo.EdmType, edmDependentInfo);
                    edmManyToManyProperty = new OeEdmNavigationShadowProperty(edmNavigationProperty, many);
                    dependentInfo.EdmType.AddProperty(edmManyToManyProperty);
                }
                else
                {
                    edmManyToManyProperty = dependentInfo.EdmType.AddUnidirectionalNavigation(edmDependentInfo);
                }

                var targetNavigationProperty  = (IEdmNavigationProperty)_entityTypeInfos[join.DeclaringType].EdmType.GetPropertyIgnoreCase(join.Name);
                var manyToManyJoinDescription = new ManyToManyJoinDescription(join.DeclaringType, joinNavigationProperty, targetNavigationProperty);
                _edmModel.SetAnnotationValue(edmManyToManyProperty, manyToManyJoinDescription);
            }
        }
        private void RegisterMasters()
        {
            foreach (Type dataObjectType in _metadata.Types)
            {
                DataObjectEdmTypeSettings typeSettings  = _metadata[dataObjectType];
                EdmEntityType             edmEntityType = _registeredEdmEntityTypes[dataObjectType];

                foreach (var masterProperty in typeSettings.MasterProperties)
                {
                    if (!_registeredEdmEntityTypes.ContainsKey(masterProperty.Value.MasterType))
                    {
                        throw new Exception($"Тип мастера {masterProperty.Value.MasterType.FullName} не найден для типа {dataObjectType.FullName}.");
                    }

                    EdmEntityType edmTargetEntityType = _registeredEdmEntityTypes[masterProperty.Value.MasterType];
                    bool          allowNull           = masterProperty.Value.AllowNull;

                    var navigationProperty = new EdmNavigationPropertyInfo
                    {
                        Name               = GetEntityPropertName(masterProperty.Key),
                        Target             = edmTargetEntityType,
                        TargetMultiplicity = allowNull
                            ? EdmMultiplicity.ZeroOrOne
                            : EdmMultiplicity.One
                    };

                    EdmNavigationProperty unidirectionalNavigation = edmEntityType.AddUnidirectionalNavigation(navigationProperty);
                    this.SetAnnotationValue(unidirectionalNavigation, new ClrPropertyInfoAnnotation(masterProperty.Key));

                    EdmEntitySet thisEdmEntitySet   = _registeredEntitySets[dataObjectType];
                    EdmEntitySet targetEdmEntitySet = _registeredEntitySets[masterProperty.Value.MasterType];
                    thisEdmEntitySet.AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);

                    // Add relation for all derived types.
                    if (_typeHierarchy.ContainsKey(dataObjectType))
                    {
                        foreach (Type derivedDataObjectType in _typeHierarchy[dataObjectType])
                        {
                            GetEdmEntitySet(derivedDataObjectType).AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);
                        }
                    }
                }
            }
        }
Esempio n. 19
0
        private IEdmNavigationProperty AddDependentNavigation(EdmNavigationPropertyInfo edmDependentInfo, bool forceShadow)
        {
            IEdmNavigationProperty edmNavigationProperty = Microsoft.OData.Edm.EdmNavigationProperty.CreateNavigationProperty(DependentInfo.EdmType, edmDependentInfo);

            if (DependentNavigationProperty is OeShadowPropertyInfo || forceShadow)
            {
                if (DependentNavigationProperty == null)
                {
                    var shadowPropertyInfo = new OeShadowPropertyInfo(DependentInfo.ClrType, PrincipalInfo.ClrType, edmNavigationProperty.Name);
                    edmNavigationProperty = new OeEdmNavigationShadowProperty(edmNavigationProperty, shadowPropertyInfo);
                }
                else
                {
                    edmNavigationProperty = new OeEdmNavigationShadowProperty(edmNavigationProperty, DependentNavigationProperty);
                }
            }

            DependentInfo.EdmType.AddProperty(edmNavigationProperty);
            return(edmNavigationProperty);
        }
        private void RegisterDetails()
        {
            foreach (Type dataObjectType in _metadata.Types)
            {
                DataObjectEdmTypeSettings typeSettings  = _metadata[dataObjectType];
                EdmEntityType             edmEntityType = _registeredEdmEntityTypes[dataObjectType];

                foreach (var detailProperty in typeSettings.DetailProperties)
                {
                    if (!_registeredEdmEntityTypes.ContainsKey(detailProperty.Value.DetailType))
                    {
                        throw new Exception($"Тип детейла {detailProperty.Value.DetailType.FullName} не найден для типа {dataObjectType.FullName}.");
                    }

                    EdmEntityType edmTargetEntityType = _registeredEdmEntityTypes[detailProperty.Value.DetailType];

                    var navigationProperty = new EdmNavigationPropertyInfo
                    {
                        Name               = GetEntityPropertName(detailProperty.Key),
                        Target             = edmTargetEntityType,
                        TargetMultiplicity = EdmMultiplicity.Many
                    };

                    EdmNavigationProperty unidirectionalNavigation = edmEntityType.AddUnidirectionalNavigation(navigationProperty);
                    this.SetAnnotationValue(unidirectionalNavigation, new ClrPropertyInfoAnnotation(detailProperty.Key));

                    EdmEntitySet thisEdmEntitySet   = _registeredEntitySets[dataObjectType];
                    EdmEntitySet targetEdmEntitySet = _registeredEntitySets[detailProperty.Value.DetailType];
                    thisEdmEntitySet.AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);

                    // Add relation for all derived types.
                    if (_typeHierarchy.ContainsKey(dataObjectType))
                    {
                        foreach (Type derivedDataObjectType in _typeHierarchy[dataObjectType])
                        {
                            GetEdmEntitySet(derivedDataObjectType).AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);
                        }
                    }
                }
            }
        }
Esempio n. 21
0
        private IEdmNavigationProperty AddPrincipalNavigation(EdmNavigationPropertyInfo edmPrincipalInfo, bool forceShadow)
        {
            IEdmNavigationProperty edmNavigationProperty = Microsoft.OData.Edm.EdmNavigationProperty.CreateNavigationProperty(PrincipalInfo.EdmType, edmPrincipalInfo);

            if (PrincipalNavigationProperty is OeShadowPropertyInfo || forceShadow)
            {
                if (PrincipalNavigationProperty == null)
                {
                    Type propertyType       = edmNavigationProperty.Type.IsCollection() ? typeof(ICollection <>).MakeGenericType(DependentInfo.ClrType) : DependentInfo.ClrType;
                    var  shadowPropertyInfo = new OeShadowPropertyInfo(PrincipalInfo.ClrType, propertyType, edmNavigationProperty.Name);
                    edmNavigationProperty = new OeEdmNavigationShadowProperty(edmNavigationProperty, shadowPropertyInfo);
                }
                else
                {
                    edmNavigationProperty = new OeEdmNavigationShadowProperty(edmNavigationProperty, PrincipalNavigationProperty);
                }
            }

            PrincipalInfo.EdmType.AddProperty(edmNavigationProperty);
            return(edmNavigationProperty);
        }
Esempio n. 22
0
        public static IEdmModel GetEdmModel()
        {
            EdmModel model = new EdmModel();

            // Create and add product entity type.
            EdmEntityType product = new EdmEntityType("NS", "Product");

            product.AddKeys(product.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            product.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            product.AddStructuralProperty("Price", EdmPrimitiveTypeKind.Double);
            model.AddElement(product);

            // Create and add category entity type.
            EdmEntityType category = new EdmEntityType("NS", "Category");

            category.AddKeys(category.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            category.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(category);

            // Set navigation from product to category.
            EdmNavigationPropertyInfo propertyInfo = new EdmNavigationPropertyInfo();

            propertyInfo.Name = "Category";
            propertyInfo.TargetMultiplicity = EdmMultiplicity.One;
            propertyInfo.Target             = category;
            EdmNavigationProperty productCategory = product.AddUnidirectionalNavigation(propertyInfo);

            // Create and add entity container.
            EdmEntityContainer container = new EdmEntityContainer("NS", "DefaultContainer");

            model.AddElement(container);

            // Create and add entity set for product and category.
            EdmEntitySet products   = container.AddEntitySet("Products", product);
            EdmEntitySet categories = container.AddEntitySet("Categories", category);

            products.AddNavigationTarget(productCategory, categories);

            return(model);
        }
Esempio n. 23
0
        private static void ReferentialConstraintDemo()
        {
            Console.WriteLine("ReferentialConstraintDemo");

            EdmModel model    = new EdmModel();
            var      customer = new EdmEntityType("ns", "Customer");

            model.AddElement(customer);
            var customerId = customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false);

            customer.AddKeys(customerId);
            var address = new EdmComplexType("ns", "Address");

            model.AddElement(address);
            var code = address.AddStructuralProperty("gid", EdmPrimitiveTypeKind.Guid);

            customer.AddStructuralProperty("addr", new EdmComplexTypeReference(address, true));

            var order = new EdmEntityType("ns", "Order");

            model.AddElement(order);
            var oId = order.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false);

            order.AddKeys(oId);

            var orderCustomerId = order.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32, false);

            var nav = new EdmNavigationPropertyInfo()
            {
                Name                = "NavCustomer",
                Target              = customer,
                TargetMultiplicity  = EdmMultiplicity.One,
                DependentProperties = new[] { orderCustomerId },
                PrincipalProperties = new[] { customerId }
            };

            order.AddUnidirectionalNavigation(nav);

            ShowModel(model);
        }
Esempio n. 24
0
        public void ParseComputeAsExpandQueryOption()
        {
            // Create model
            EdmModel         model         = new EdmModel();
            EdmEntityType    elementType   = model.AddEntityType("DevHarness", "Entity");
            EdmEntityType    targetType    = model.AddEntityType("DevHarness", "Navigation");
            EdmTypeReference typeReference = new EdmStringTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), false);

            targetType.AddProperty(new EdmStructuralProperty(targetType, "Prop1", typeReference));
            EdmNavigationPropertyInfo propertyInfo = new EdmNavigationPropertyInfo();

            propertyInfo.Name               = "Nav1";
            propertyInfo.Target             = targetType;
            propertyInfo.TargetMultiplicity = EdmMultiplicity.One;
            EdmProperty navigation = EdmNavigationProperty.CreateNavigationProperty(elementType, propertyInfo);

            elementType.AddProperty(navigation);

            EdmEntityContainer container = model.AddEntityContainer("Default", "Container");

            container.AddEntitySet("Entities", elementType);

            // Define queries and new up parser.
            Uri            root   = new Uri("http://host");
            Uri            url    = new Uri("http://host/Entities?$expand=Nav1($compute=cast(Prop1, 'Edm.String') as NavProperty1AsString)");
            ODataUriParser parser = new ODataUriParser(model, root, url);

            // parse and validate
            SelectExpandClause clause = parser.ParseSelectAndExpand();
            List <SelectItem>  items  = clause.SelectedItems.ToList();

            items.Count.Should().Be(1);
            ExpandedNavigationSelectItem expanded = items[0] as ExpandedNavigationSelectItem;
            List <ComputeExpression>     computes = expanded.ComputeOption.ComputedItems.ToList();

            computes.Count.Should().Be(1);
            computes[0].Alias.ShouldBeEquivalentTo("NavProperty1AsString");
            computes[0].Expression.ShouldBeSingleValueFunctionCallQueryNode();
            computes[0].Expression.TypeReference.ShouldBeEquivalentTo(typeReference);
        }
Esempio n. 25
0
        public void Build(EntityTypeInfo typeInfo)
        {
            (PropertyInfo Many, PropertyInfo Join)manyToManyInfo = GetManyToManyInfo(_metadataProvider, typeInfo.ClrType);
            if (manyToManyInfo.Many == null)
            {
                return;
            }

            IEdmNavigationProperty joinNavigationProperty = GetJoinNavigationProperty(typeInfo, manyToManyInfo.Join.DeclaringType);

            if (joinNavigationProperty == null)
            {
                return;
            }

            IEdmNavigationProperty targetNavigationProperty = GetTargetNavigationProperty(_entityTypeInfos[manyToManyInfo.Join.DeclaringType], manyToManyInfo.Join.PropertyType);

            if (targetNavigationProperty == null)
            {
                return;
            }

            EntityTypeInfo principalInfo    = _entityTypeInfos[manyToManyInfo.Join.PropertyType];
            EntityTypeInfo dependentInfo    = _entityTypeInfos[manyToManyInfo.Many.DeclaringType];
            var            edmDependentInfo = new EdmNavigationPropertyInfo()
            {
                ContainsTarget      = true,
                Name                = manyToManyInfo.Many.Name,
                OnDelete            = EdmOnDeleteAction.None,
                PrincipalProperties = principalInfo.EdmType.DeclaredKey,
                Target              = principalInfo.EdmType,
                TargetMultiplicity  = EdmMultiplicity.Many
            };
            EdmNavigationProperty edmManyToManyProperty = dependentInfo.EdmType.AddUnidirectionalNavigation(edmDependentInfo);

            var manyToManyJoinDescription = new ManyToManyJoinDescription(manyToManyInfo.Join.DeclaringType, joinNavigationProperty, targetNavigationProperty);

            _edmModel.SetAnnotationValue(edmManyToManyProperty, manyToManyJoinDescription);
        }
Esempio n. 26
0
        private IEdmNavigationProperty AddBidirectionalNavigation(EdmNavigationPropertyInfo edmDependentInfo, EdmNavigationPropertyInfo edmPrincipalInfo)
        {
            IEdmNavigationProperty dependentNavigationProperty;

            if (DependentNavigationProperty is OeShadowPropertyInfo || PrincipalNavigationProperty is OeShadowPropertyInfo)
            {
                var dependentShadowProperty = (OeEdmNavigationShadowProperty)AddDependentNavigation(edmDependentInfo, true);
                var principalShadowProperty = (OeEdmNavigationShadowProperty)AddPrincipalNavigation(edmPrincipalInfo, true);

                dependentShadowProperty.SetPartner(principalShadowProperty);
                principalShadowProperty.SetPartner(dependentShadowProperty);

                dependentNavigationProperty = dependentShadowProperty;
            }
            else
            {
                dependentNavigationProperty = Microsoft.OData.Edm.EdmNavigationProperty.CreateNavigationPropertyWithPartner(edmDependentInfo, edmPrincipalInfo);
                DependentInfo.EdmType.AddProperty(dependentNavigationProperty);
                PrincipalInfo.EdmType.AddProperty(dependentNavigationProperty.Partner);
            }

            return(dependentNavigationProperty);
        }
Esempio n. 27
0
        public static void UpgradeBidirectionalNavigationProperties(Microsoft.OData.Edm.IEdmModel model, string setName, string partnerSetName, string elementTypeName, string partnerElementTypeName, string navigationName, string partnerName, EdmMultiplicity multipl, EdmMultiplicity partnerMultipl)
        {
            var fromSet    = (EdmEntitySet)model.EntityContainer.FindEntitySet(setName);
            var partnerSet = (EdmEntitySet)model.EntityContainer.FindEntitySet(partnerSetName);

            var fromType    = (EdmEntityType)model.FindDeclaredType(elementTypeName);
            var partnerType = (EdmEntityType)model.FindDeclaredType(partnerElementTypeName);

            if (fromType == null)
            {
                throw new Exception("fromType is null");
            }

            if (partnerType == null)
            {
                throw new Exception("partnerType is null");
            }

            var partsProperty = new EdmNavigationPropertyInfo();

            partsProperty.Name = navigationName;
            partsProperty.TargetMultiplicity = multipl;
            partsProperty.Target             = partnerType;
            partsProperty.ContainsTarget     = false;
            partsProperty.OnDelete           = EdmOnDeleteAction.None;

            var partnerProperty = new EdmNavigationPropertyInfo();

            partnerProperty.Name = partnerName;
            partnerProperty.TargetMultiplicity = partnerMultipl;
            partnerProperty.Target             = fromType;
            partnerProperty.ContainsTarget     = false;
            partnerProperty.OnDelete           = EdmOnDeleteAction.None;

            fromSet.AddNavigationTarget(fromType.AddBidirectionalNavigation(partsProperty, partnerProperty), partnerSet);
        }
Esempio n. 28
0
        private void CreateNavigationProperty(EntityTypeConfiguration config)
        {
            Contract.Assert(config != null);

            EdmEntityType entityType = (EdmEntityType)(_types[config.ClrType]);

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo();
                info.Name = navProp.Name;
                info.TargetMultiplicity = navProp.Multiplicity;
                info.Target             = _types[navProp.RelatedClrType] as IEdmEntityType;
                info.OnDelete           = navProp.OnDeleteAction;

                if (navProp.ReferentialConstraint.Any())
                {
                    info.DependentProperties = ReorderDependentProperties(navProp);
                }

                //TODO: If target end has a multiplity of 1 this assumes the source end is 0..1.
                //      I think a better default multiplicity is *
                entityType.AddUnidirectionalNavigation(info);
            }
        }
Esempio n. 29
0
        public void EdmNavigationPropertyPrincipalPropertiesShouldReturnPrincipalProperties()
        {
            EdmEntityType type   = new EdmEntityType("ns", "type");
            var           key    = type.AddStructuralProperty("Id1", EdmCoreModel.Instance.GetInt32(false));
            var           notKey = type.AddStructuralProperty("Id2", EdmCoreModel.Instance.GetString(false));
            var           p1     = type.AddStructuralProperty("p1", EdmCoreModel.Instance.GetInt32(false));
            var           p2     = type.AddStructuralProperty("p1", EdmCoreModel.Instance.GetString(false));

            type.AddKeys(key);

            var navInfo1 = new EdmNavigationPropertyInfo()
            {
                Name                = "nav",
                Target              = type,
                TargetMultiplicity  = EdmMultiplicity.Many,
                DependentProperties = new[] { p1, p2 },
                PrincipalProperties = new[] { key, notKey }
            };

            EdmNavigationProperty navProp = type.AddUnidirectionalNavigation(navInfo1);

            navProp.PrincipalProperties().Should().NotBeNull();
            navProp.PrincipalProperties().ShouldAllBeEquivalentTo(new[] { key, notKey });
        }
Esempio n. 30
0
        private static void AddNavigationProperties(
            IModel efModel,
            INavigation navigation,
            EdmModel model,
            IDictionary <IAnnotatable, IEdmElement> elementMap)
        {
            if (!navigation.PointsToPrincipal())
            {
                return;
            }

            var naviPair         = new INavigation[] { navigation, navigation.FindInverse() };
            var navPropertyInfos = new EdmNavigationPropertyInfo[2];

            for (var i = 0; i < 2; i++)
            {
                var navi = naviPair[i];
                if (navi == null)
                {
                    continue;
                }

                var efEntityType = navi.DeclaringEntityType;
                if (!elementMap.ContainsKey(efEntityType))
                {
                    continue;
                }

                var entityType         = elementMap[efEntityType] as IEdmEntityType;
                var efTargetEntityType = navi.GetTargetType();
                if (!elementMap.ContainsKey(efTargetEntityType))
                {
                    continue;
                }

                var targetEntityType = elementMap[efTargetEntityType] as IEdmEntityType;
                navPropertyInfos[i] = new EdmNavigationPropertyInfo()
                {
                    ContainsTarget     = false,
                    Name               = navi.Name,
                    Target             = targetEntityType,
                    TargetMultiplicity = ModelProducer.GetEdmMultiplicity(navi),
                };
                var foreignKey = navi.ForeignKey;
                if (foreignKey != null && navi.PointsToPrincipal())
                {
                    navPropertyInfos[i].OnDelete = foreignKey.DeleteBehavior == DeleteBehavior.Cascade ?
                                                   EdmOnDeleteAction.Cascade : EdmOnDeleteAction.None;
                    navPropertyInfos[i].DependentProperties = foreignKey.Properties
                                                              .Select(p => entityType.FindProperty(p.Name) as IEdmStructuralProperty);
                    navPropertyInfos[i].PrincipalProperties = foreignKey.PrincipalKey.Properties
                                                              .Select(p => targetEntityType.FindProperty(p.Name) as IEdmStructuralProperty);
                }
            }

            if (navPropertyInfos[0] == null && navPropertyInfos[1] != null)
            {
                var efEntityType = navigation.GetTargetType();
                var entityType   = elementMap[efEntityType] as EdmEntityType;
                if (entityType.FindProperty(navPropertyInfos[1].Name) == null)
                {
                    entityType.AddUnidirectionalNavigation(navPropertyInfos[1]);
                }
            }

            if (navPropertyInfos[0] != null && navPropertyInfos[1] == null)
            {
                var efEntityType = navigation.DeclaringEntityType;
                var entityType   = elementMap[efEntityType] as EdmEntityType;
                if (entityType.FindProperty(navPropertyInfos[0].Name) == null)
                {
                    entityType.AddUnidirectionalNavigation(navPropertyInfos[0]);
                }
            }

            if (navPropertyInfos[0] != null && navPropertyInfos[1] != null)
            {
                var efEntityType = navigation.DeclaringEntityType;
                var entityType   = elementMap[efEntityType] as EdmEntityType;
                if (entityType.FindProperty(navPropertyInfos[0].Name) == null)
                {
                    entityType.AddBidirectionalNavigation(
                        navPropertyInfos[0], navPropertyInfos[1]);
                }
            }
        }