Пример #1
0
        public static EdmModel NavigationPropertyWithAnnotationModel()
        {
            EdmModel model = new EdmModel();

            EdmEntityType         person   = new EdmEntityType("DefaultNamespace", "Person");
            EdmStructuralProperty personId = person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));

            person.AddKeys(personId);
            model.AddElement(person);

            EdmNavigationProperty friend = person.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name = "Friends", Target = person, TargetMultiplicity = EdmMultiplicity.One
            },
                new EdmNavigationPropertyInfo()
            {
                Name = "Self", TargetMultiplicity = EdmMultiplicity.One
            });
            IEdmNavigationProperty self = friend.Partner;

            XElement annotationElement =
                new XElement("{http://foo}Annotation", "1");
            var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString());

            annotation.SetIsSerializedAsElement(model, true);
            model.SetAnnotationValue(friend, "http://foo", "Annotation", annotation);

            return(model);
        }
Пример #2
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));
        }
Пример #3
0
        public SelectedPropertiesNodeTests()
        {
            this.edmModel = new EdmModel();

            this.defaultContainer = new EdmEntityContainer("TestModel", "DefaultContainer");
            this.edmModel.AddElement(this.defaultContainer);

            this.townType = new EdmEntityType("TestModel", "Town");
            this.edmModel.AddElement(townType);
            EdmStructuralProperty townIdProperty = townType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));

            townType.AddKeys(townIdProperty);
            townType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            townType.AddStructuralProperty("Size", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));

            this.cityType = new EdmEntityType("TestModel", "City", this.townType);
            cityType.AddStructuralProperty("Photo", EdmCoreModel.Instance.GetStream(/*isNullable*/ false));
            this.edmModel.AddElement(cityType);

            this.districtType = new EdmEntityType("TestModel", "District");
            EdmStructuralProperty districtIdProperty = districtType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));

            districtType.AddKeys(districtIdProperty);
            districtType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));
            districtType.AddStructuralProperty("Thumbnail", EdmCoreModel.Instance.GetStream(/*isNullable*/ false));
            this.edmModel.AddElement(districtType);

            cityType.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo {
                Name = "Districts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo {
                Name = "City", Target = cityType, TargetMultiplicity = EdmMultiplicity.One
            });

            this.defaultContainer.AddEntitySet("Cities", cityType);
            this.defaultContainer.AddEntitySet("Districts", districtType);

            this.metropolisType = new EdmEntityType("TestModel", "Metropolis", this.cityType);
            this.metropolisType.AddStructuralProperty("MetropolisStream", EdmCoreModel.Instance.GetStream(/*isNullable*/ false));
            this.edmModel.AddElement(metropolisType);

            metropolisType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "MetropolisNavigation", Target = districtType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne
            });

            this.action = new EdmAction("TestModel", "Action", new EdmEntityTypeReference(this.cityType, true));
            this.action.AddParameter("Param", EdmCoreModel.Instance.GetInt32(false));
            this.edmModel.AddElement(action);
            this.actionImport = new EdmActionImport(this.defaultContainer, "Action", action);

            this.actionConflictingWithPropertyName = new EdmAction("TestModel", "Zip", new EdmEntityTypeReference(this.districtType, true));
            this.actionConflictingWithPropertyName.AddParameter("Param", EdmCoreModel.Instance.GetInt32(false));
            this.edmModel.AddElement(actionConflictingWithPropertyName);
            this.actionImportConflictingWithPropertyName = new EdmActionImport(this.defaultContainer, "Zip", actionConflictingWithPropertyName);

            this.openType = new EdmEntityType("TestModel", "OpenCity", this.cityType, false, true);
        }
Пример #4
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);
                }
            }
        }
Пример #5
0
        private void InitializeEdmModel()
        {
            this.edmModel = new EdmModel();

            EdmEntityContainer defaultContainer = new EdmEntityContainer("TestModel", "DefaultContainer");

            this.edmModel.AddElement(defaultContainer);

            EdmComplexType addressType = new EdmComplexType("TestModel", "Address");

            addressType.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            addressType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetString(/*isNullable*/ false));

            this.cityType = new EdmEntityType("TestModel", "City");
            EdmStructuralProperty cityIdProperty = cityType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));

            cityType.AddKeys(cityIdProperty);
            cityType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            cityType.AddStructuralProperty("Size", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));
            cityType.AddStructuralProperty("Restaurants", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(/*isNullable*/ false)));
            cityType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, true));
            this.edmModel.AddElement(cityType);

            this.capitolCityType = new EdmEntityType("TestModel", "CapitolCity", cityType);
            capitolCityType.AddStructuralProperty("CapitolType", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            this.edmModel.AddElement(capitolCityType);

            EdmEntityType         districtType       = new EdmEntityType("TestModel", "District");
            EdmStructuralProperty districtIdProperty = districtType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));

            districtType.AddKeys(districtIdProperty);
            districtType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            districtType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));
            this.edmModel.AddElement(districtType);

            cityType.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo {
                Name = "Districts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo {
                Name = "City", Target = cityType, TargetMultiplicity = EdmMultiplicity.One
            });

            cityType.NavigationProperties().Single(np => np.Name == "Districts");
            capitolCityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "CapitolDistrict", Target = districtType, TargetMultiplicity = EdmMultiplicity.One
            });
            capitolCityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "OutlyingDistricts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many
            });

            this.citySet = defaultContainer.AddEntitySet("Cities", cityType);
            defaultContainer.AddEntitySet("Districts", districtType);

            this.singletonCity = defaultContainer.AddSingleton("SingletonCity", cityType);
        }
Пример #6
0
        public void DollarMetadata_Works_WithMultipleReferentialConstraints_ForUntypedModel()
        {
            // Arrange
            EdmModel model = new EdmModel();

            EdmEntityType customer = new EdmEntityType("DefaultNamespace", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmCoreModel.Instance.GetInt32(false)));
            customer.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
            model.AddElement(customer);

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

            order.AddKeys(order.AddStructuralProperty("OrderId", EdmCoreModel.Instance.GetInt32(false)));
            EdmStructuralProperty orderCustomerId = order.AddStructuralProperty("CustomerForeignKey", EdmCoreModel.Instance.GetInt32(true));

            model.AddElement(order);

            customer.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name               = "Orders",
                Target             = order,
                TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo
            {
                Name = "Customer",
                TargetMultiplicity  = EdmMultiplicity.ZeroOrOne,
                DependentProperties = new[] { orderCustomerId },
            });

            const string expect =
                "        <ReferentialConstraint>\r\n" +
                "          <Principal Role=\"Customer\">\r\n" +
                "            <PropertyRef Name=\"CustomerId\" />\r\n" +
                "          </Principal>\r\n" +
                "          <Dependent Role=\"Orders\">\r\n" +
                "            <PropertyRef Name=\"CustomerForeignKey\" />\r\n" +
                "          </Dependent>\r\n" +
                "        </ReferentialConstraint>";

            HttpServer server = new HttpServer();

            server.Configuration.Routes.MapODataServiceRoute("odata", "odata", model);

            HttpClient client = new HttpClient(server);

            // Act
            HttpResponseMessage response = client.GetAsync("http://localhost/odata/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);
            Assert.Contains(expect, response.Content.ReadAsStringAsync().Result);
        }
Пример #7
0
        public void DollarMetadata_Works_WithMultipleReferentialConstraints_ForUntypeModel()
        {
            // Arrange
            EdmModel model = new EdmModel();

            EdmEntityType customer = new EdmEntityType("DefaultNamespace", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmCoreModel.Instance.GetInt32(isNullable: false)));
            customer.AddStructuralProperty("Name",
                                           EdmCoreModel.Instance.GetString(isUnbounded: false, maxLength: 100, isUnicode: null, isNullable: false));
            model.AddElement(customer);

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

            order.AddKeys(order.AddStructuralProperty("OrderId", EdmCoreModel.Instance.GetInt32(false)));
            EdmStructuralProperty orderCustomerId = order.AddStructuralProperty("CustomerForeignKey", EdmCoreModel.Instance.GetInt32(true));

            model.AddElement(order);

            customer.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "Orders", Target = order, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo
            {
                Name = "Customer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                DependentProperties = new[] { orderCustomerId },
                PrincipalProperties = customer.Key()
            });

            const string expect =
                "        <Property Name=\"CustomerForeignKey\" Type=\"Edm.Int32\" />\r\n" +
                "        <NavigationProperty Name=\"Customer\" Type=\"DefaultNamespace.Customer\" Partner=\"Orders\">\r\n" +
                "          <ReferentialConstraint Property=\"CustomerForeignKey\" ReferencedProperty=\"CustomerId\" />\r\n" +
                "        </NavigationProperty>";

            HttpConfiguration config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            HttpServer        server = new HttpServer(config);

            config.MapODataServiceRoute("odata", "odata", model);

            HttpClient client = new HttpClient(server);

            // Act
            HttpResponseMessage response = client.GetAsync("http://localhost/odata/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);
            Assert.Contains(expect, response.Content.ReadAsStringAsync().Result);
        }
Пример #8
0
        public void EdmSingletonBasicNavigationPropertyBindingTest()
        {
            var internalOrderProperty = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "InternalOrder", Target = this.orderType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne
            });
            var externalOrderProperty = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "ExternalOrders", Target = this.orderType, TargetMultiplicity = EdmMultiplicity.Many
            });
            var customerProductProperty = customerType.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name = "Products", Target = this.productType, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo()
            {
                Name = "Buyer", Target = this.customerType, TargetMultiplicity = EdmMultiplicity.One
            });


            var orderSet   = new EdmEntitySet(this.entityContainer, "Orders", this.orderType);
            var productSet = new EdmEntitySet(this.entityContainer, "Products", this.productType);

            var vipCustomer = new EdmSingleton(this.entityContainer, "VIP", this.customerType);

            Assert.Empty(vipCustomer.NavigationPropertyBindings);

            vipCustomer.AddNavigationTarget(internalOrderProperty, orderSet);
            vipCustomer.AddNavigationTarget(externalOrderProperty, orderSet);
            vipCustomer.AddNavigationTarget(customerProductProperty, productSet);

            Assert.Equal(3, vipCustomer.NavigationPropertyBindings.Count());
            Assert.Contains(vipCustomer.NavigationPropertyBindings, m => m.NavigationProperty == internalOrderProperty && m.Target == orderSet);
            Assert.Contains(vipCustomer.NavigationPropertyBindings, m => m.NavigationProperty == externalOrderProperty && m.Target == orderSet);
            Assert.Contains(vipCustomer.NavigationPropertyBindings, m => m.NavigationProperty == customerProductProperty && m.Target == productSet);

            Assert.Same(orderSet, vipCustomer.FindNavigationTarget(internalOrderProperty));
            Assert.Same(orderSet, vipCustomer.FindNavigationTarget(externalOrderProperty));
            Assert.Same(productSet, vipCustomer.FindNavigationTarget(customerProductProperty));

            productSet.AddNavigationTarget(customerProductProperty.Partner, vipCustomer);
            Assert.Same(vipCustomer, productSet.FindNavigationTarget(customerProductProperty.Partner));
        }
        private static EdmModel BuildEdmModel()
        {
            var model = new EdmModel();

            var numberEntity = new EdmEntityType("NS", "Number");

            numberEntity.AddKeys(numberEntity.AddStructuralProperty("RowId", EdmCoreModel.Instance.GetInt32(false)));
            numberEntity.AddStructuralProperty("RowParity", EdmCoreModel.Instance.GetString(false));
            numberEntity.AddStructuralProperty("RowCategory", EdmCoreModel.Instance.GetString(false));
            numberEntity.AddStructuralProperty("IntProp", EdmCoreModel.Instance.GetInt32(false));
            numberEntity.AddStructuralProperty("NullableIntProp", EdmCoreModel.Instance.GetInt32(true));
            numberEntity.AddStructuralProperty("DoubleProp", EdmCoreModel.Instance.GetDouble(false));
            numberEntity.AddStructuralProperty("NullableDoubleProp", EdmCoreModel.Instance.GetDouble(true));
            numberEntity.AddStructuralProperty("DecimalProp", EdmCoreModel.Instance.GetDecimal(false));
            numberEntity.AddStructuralProperty("NullableDecimalProp", EdmCoreModel.Instance.GetDecimal(true));
            numberEntity.AddStructuralProperty("LongProp", EdmCoreModel.Instance.GetInt64(false));
            numberEntity.AddStructuralProperty("NullableLongProp", EdmCoreModel.Instance.GetInt64(true));
            numberEntity.AddStructuralProperty("SingleProp", EdmCoreModel.Instance.GetSingle(false));
            numberEntity.AddStructuralProperty("NullableSingleProp", EdmCoreModel.Instance.GetSingle(true));

            var saleEntity = new EdmEntityType("NS", "Sale");

            saleEntity.AddKeys(saleEntity.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)));
            saleEntity.AddStructuralProperty("CustomerId", EdmCoreModel.Instance.GetString(false));
            saleEntity.AddStructuralProperty("Date", EdmCoreModel.Instance.GetString(false));
            saleEntity.AddStructuralProperty("ProductId", EdmCoreModel.Instance.GetString(false));
            saleEntity.AddStructuralProperty("CurrencyCode", EdmCoreModel.Instance.GetString(false));
            saleEntity.AddStructuralProperty("Amount", EdmCoreModel.Instance.GetDecimal(false));

            var productEntity = new EdmEntityType("NS", "Product");

            productEntity.AddKeys(productEntity.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)));
            productEntity.AddStructuralProperty("CategoryId", EdmCoreModel.Instance.GetString(false));
            productEntity.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
            productEntity.AddStructuralProperty("Color", EdmCoreModel.Instance.GetString(false));
            productEntity.AddStructuralProperty("TaxRate", EdmCoreModel.Instance.GetDecimal(false));

            var customerEntity = new EdmEntityType("NS", "Customer");

            customerEntity.AddKeys(customerEntity.AddStructuralProperty("Id", EdmCoreModel.Instance.GetString(false)));
            customerEntity.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
            customerEntity.AddStructuralProperty("Country", EdmCoreModel.Instance.GetString(false));

            var categoryEntity = new EdmEntityType("NS", "Category");

            categoryEntity.AddKeys(categoryEntity.AddStructuralProperty("Id", EdmCoreModel.Instance.GetString(false)));
            categoryEntity.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));

            var currencyEntity = new EdmEntityType("NS", "Currency");

            currencyEntity.AddKeys(currencyEntity.AddStructuralProperty("Code", EdmCoreModel.Instance.GetString(false)));
            currencyEntity.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));

            // Associations
            saleEntity.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo {
                Name = "Customer", Target = customerEntity, TargetMultiplicity = EdmMultiplicity.One
            },
                new EdmNavigationPropertyInfo {
                Name = "Sales", Target = saleEntity, TargetMultiplicity = EdmMultiplicity.Many
            });
            saleEntity.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo {
                Name = "Product", Target = productEntity, TargetMultiplicity = EdmMultiplicity.One
            },
                new EdmNavigationPropertyInfo {
                Name = "Sales", Target = saleEntity, TargetMultiplicity = EdmMultiplicity.Many
            });
            saleEntity.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo {
                Name = "Currency", Target = currencyEntity, TargetMultiplicity = EdmMultiplicity.One
            });

            productEntity.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo {
                Name = "Category", Target = categoryEntity, TargetMultiplicity = EdmMultiplicity.One
            },
                new EdmNavigationPropertyInfo {
                Name = "Products", Target = productEntity, TargetMultiplicity = EdmMultiplicity.Many
            });

            var entityContainer = new EdmEntityContainer("NS", "Container");

            model.AddElement(numberEntity);
            model.AddElement(saleEntity);
            model.AddElement(productEntity);
            model.AddElement(customerEntity);
            model.AddElement(categoryEntity);
            model.AddElement(currencyEntity);
            model.AddElement(entityContainer);

            entityContainer.AddEntitySet(numbersEntitySetName, numberEntity);
            entityContainer.AddEntitySet(salesEntitySetName, saleEntity);
            entityContainer.AddEntitySet(productsEntitySetName, productEntity);

            return(model);
        }
Пример #10
0
        private void CreateNavigationProperty(StructuralTypeConfiguration config)
        {
            Contract.Assert(config != null);

            EdmStructuredType type = (EdmStructuredType)(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);
                    }

                    return(info);
                };

                var           navInfo    = getInfo(navProp);
                var           props      = new Dictionary <IEdmProperty, NavigationPropertyConfiguration>();
                EdmEntityType entityType = type as EdmEntityType;
                if (entityType != null && navProp.Partner != null)
                {
                    var edmProperty        = entityType.AddBidirectionalNavigation(navInfo, getInfo(navProp.Partner));
                    var partnerEdmProperty = (navInfo.Target as EdmEntityType).Properties().Single(p => p.Name == navProp.Partner.Name);
                    props.Add(edmProperty, navProp);
                    props.Add(partnerEdmProperty, navProp.Partner);
                }
                else
                {
                    // Do not add this if we have have a partner relationship configured, as this
                    // property will be added automatically through the AddBidirectionalNavigation
                    var targetConfig = config.ModelBuilder.GetTypeConfigurationOrNull(navProp.RelatedClrType) as StructuralTypeConfiguration;
                    if (!targetConfig.NavigationProperties.Any(p => p.Partner != null && p.Partner.Name == navInfo.Name))
                    {
                        var edmProperty = type.AddUnidirectionalNavigation(navInfo);
                        props.Add(edmProperty, navProp);
                    }
                }

                foreach (var item in props)
                {
                    var edmProperty = item.Key;
                    var prop        = item.Value;
                    if (prop.PropertyInfo != null)
                    {
                        _properties[prop.PropertyInfo] = edmProperty;
                    }


                    if (prop.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(prop);
                    }

                    /*
                     *                 if (prop.QueryConfiguration.ModelBoundQuerySettings != null)
                     *                 {
                     *                     _propertiesQuerySettings.Add(edmProperty, prop.QueryConfiguration.ModelBoundQuerySettings);
                     *                 }*/
                }
            }
        }
Пример #11
0
        private void InitializeEdmModel()
        {
            this.edmModel = new EdmModel();

            EdmEntityContainer defaultContainer = new EdmEntityContainer("TestModel", "DefaultContainer");

            this.edmModel.AddElement(defaultContainer);

            addressType = new EdmComplexType("TestModel", "Address");
            addressType.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            addressType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetString(/*isNullable*/ false));

            this.cityType = new EdmEntityType("TestModel", "City", baseType: null, isAbstract: false, isOpen: true);
            EdmStructuralProperty cityIdProperty = cityType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));

            cityType.AddKeys(cityIdProperty);
            cityType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            cityType.AddStructuralProperty("Size", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));
            cityType.AddStructuralProperty("Restaurants", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(/*isNullable*/ false)));
            cityType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, true));
            this.edmModel.AddElement(cityType);

            this.capitolCityType = new EdmEntityType("TestModel", "CapitolCity", cityType);
            capitolCityType.AddStructuralProperty("CapitolType", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            this.edmModel.AddElement(capitolCityType);

            this.districtType = new EdmEntityType("TestModel", "District");
            EdmStructuralProperty districtIdProperty = districtType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));

            districtType.AddKeys(districtIdProperty);
            districtType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(/*isNullable*/ false));
            districtType.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetInt32(/*isNullable*/ false));
            this.edmModel.AddElement(districtType);

            cityType.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo {
                Name = "Districts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo {
                Name = "City", Target = cityType, TargetMultiplicity = EdmMultiplicity.One
            });

            cityType.NavigationProperties().Single(np => np.Name == "Districts");
            capitolCityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "CapitolDistrict", Target = districtType, TargetMultiplicity = EdmMultiplicity.One
            });
            capitolCityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "OutlyingDistricts", Target = districtType, TargetMultiplicity = EdmMultiplicity.Many
            });

            this.citySet     = defaultContainer.AddEntitySet("Cities", cityType);
            this.districtSet = defaultContainer.AddEntitySet("Districts", districtType);

            this.singletonCity = defaultContainer.AddSingleton("SingletonCity", cityType);

            // operations
            var cityReference       = new EdmEntityTypeReference(cityType, true);
            var districtReference   = new EdmEntityTypeReference(districtType, true);
            IEdmPathExpression path = new EdmPathExpression("binding/Districts");
            var function            = new EdmFunction("TestModel", "GetOneDistrict", districtReference, true, path, true /*isComposable*/);

            function.AddParameter("binding", cityReference);
            edmModel.AddElement(function);
        }
Пример #12
0
        internal static IEdmModel GetEdmModel()
        {
            var model = new EdmModel();

            // open address
            var address = new EdmComplexType("NS", "Address", null, false, true);

            address.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(true));
            address.AddStructuralProperty("City", EdmCoreModel.Instance.GetString(true));
            model.AddElement(address);

            // customer
            var customer = new EdmEntityType("NS", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)));
            customer.AddStructuralProperty("HomeAddress", new EdmComplexTypeReference(address, true));
            model.AddElement(customer);

            // order
            var order = new EdmEntityType("NS", "Order");

            order.AddKeys(order.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(order);

            var nav = customer.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name               = "Orders",
                Target             = order,
                TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo
            {
                Name = "Customer",
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
            });

            var container = new EdmEntityContainer("Default", "Container");

            model.AddElement(container);

            var customers = new EdmEntitySet(container, "Customers", customer);

            container.AddElement(customers);

            var orders = new EdmEntitySet(container, "Orders", order);

            container.AddElement(orders);
            customers.AddNavigationTarget(nav, orders);

            // bound operations
            var customerReference = new EdmEntityTypeReference(customer, true);
            var orderReference    = new EdmEntityTypeReference(order, true);

            IEdmPathExpression path = new EdmPathExpression("binding/Orders");

            // entityset path & return type
            var function = new EdmFunction("NS", "GetSomeOrders", new EdmCollectionTypeReference(new EdmCollectionType(orderReference)), true, path, true /*isComposable*/);

            function.AddParameter("binding", customerReference);
            model.AddElement(function);

            function = new EdmFunction("NS", "GetAnOrder", orderReference, true, path, true /*isComposable*/);
            function.AddParameter("binding", customerReference);
            model.AddElement(function);

            // GetSomeAddressFromCustomer
            function = new EdmFunction("NS", "GetSomeAddressFromCustomer1", new EdmComplexTypeReference(address, true), true, null, true /*isComposable*/);
            function.AddParameter("binding", new EdmEntityTypeReference(customer, true));
            model.AddElement(function);

            // We leave the "GetSomeAddressFromCustomer2" code here.
            // However, the operation with the entity set path containing a complex type doesn't make sense.
            IEdmPathExpression complexPath = new EdmPathExpression("binding/HomeAddress");

            function = new EdmFunction("NS", "GetSomeAddressFromCustomer2", new EdmComplexTypeReference(address, true), true, complexPath, true /*isComposable*/);
            function.AddParameter("binding", new EdmEntityTypeReference(customer, true));
            model.AddElement(function);

            function = new EdmFunction("NS", "GetSomeAddressFromCustomer3", new EdmComplexTypeReference(address, true), true, null, true /*isComposable*/);
            function.AddParameter("binding", new EdmEntityTypeReference(customer, true));
            model.AddElement(function);

            var action = new EdmAction("NS", "DoSomeThing", null, true, null);

            action.AddParameter("binding", new EdmEntityTypeReference(customer, true));
            model.AddElement(action);

            // operation import
            function = new EdmFunction("NS", "GetSomeOrders2", new EdmCollectionTypeReference(new EdmCollectionType(orderReference)));
            EdmFunctionImport functionImport = new EdmFunctionImport(container, "GetSomeOrders2", function, new EdmPathExpression("Orders"), false);

            container.AddElement(functionImport);

            function       = new EdmFunction("NS", "GetAnOrder2", orderReference);
            functionImport = new EdmFunctionImport(container, "GetAnOrder2", function, new EdmPathExpression("Orders"), false);
            container.AddElement(functionImport);

            function       = new EdmFunction("NS", "GetSomeAddress", new EdmComplexTypeReference(address, true), false, null, true);
            functionImport = new EdmFunctionImport(container, "GetSomeAddress", function, null, false);
            container.AddElement(functionImport);

            action = new EdmAction("NS", "DoSomeThing2", null, false, null);
            var actionImport = new EdmActionImport(container, "DoSomeThing2", action, null);

            container.AddElement(actionImport);
            return(model);
        }
Пример #13
0
        /// <summary>
        /// Creates a model, used model information at http://schema.org/Person as inspiration
        /// </summary>
        /// <returns></returns>
        public static IEdmModel BuildModel(string modelNamespace)
        {
            var edmModel  = new EdmModel();
            var container = new EdmEntityContainer(modelNamespace, "Container");

            edmModel.AddElement(container);

            // Create types
            var addressType = new EdmComplexType(modelNamespace, "PostalAddress");

            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            var addressRefType = new EdmComplexTypeReference(addressType, true);

            var placeType        = new EdmEntityType(modelNamespace, "Place");
            var placeTypeKeyProp = placeType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);

            placeType.AddKeys(placeTypeKeyProp);
            placeType.AddStructuralProperty("MapUri", EdmPrimitiveTypeKind.String);
            placeType.AddStructuralProperty("TelephoneNumber", EdmPrimitiveTypeKind.String);
            placeType.AddStructuralProperty("Address", addressRefType);

            var personType        = new EdmEntityType(modelNamespace, "Person");
            var personTypeKeyProp = personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);

            personType.AddKeys(personTypeKeyProp);
            personType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            personType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            personType.AddStructuralProperty("Address", addressRefType);

            var organizationType        = new EdmEntityType(modelNamespace, "Organization");
            var organizationTypeKeyProp = organizationType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);

            organizationType.AddKeys(organizationTypeKeyProp);
            organizationType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            organizationType.AddStructuralProperty("Address", addressRefType);

            var corporationType = new EdmEntityType(modelNamespace, "Corporation", organizationType);

            corporationType.AddStructuralProperty("TickerSymbol", EdmPrimitiveTypeKind.String);

            var localBusinessType = new EdmEntityType(modelNamespace, "LocalBusiness", organizationType);

            // Create associations
            personType.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name = "Employers", Target = organizationType, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo()
            {
                Name = "Employees", Target = personType, TargetMultiplicity = EdmMultiplicity.Many
            });

            personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "CurrentPosition", Target = placeType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne
            });

            personType.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name = "Children", Target = personType, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo()
            {
                Name = "Parent", Target = personType, TargetMultiplicity = EdmMultiplicity.One
            });

            localBusinessType.AddBidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name = "LocalBusinessBranches", Target = organizationType, TargetMultiplicity = EdmMultiplicity.Many
            },
                new EdmNavigationPropertyInfo()
            {
                Name = "MainOrganization", Target = localBusinessType, TargetMultiplicity = EdmMultiplicity.One
            });

            edmModel.AddElement(addressType);
            edmModel.AddElement(personType);
            edmModel.AddElement(organizationType);
            edmModel.AddElement(corporationType);
            edmModel.AddElement(localBusinessType);
            edmModel.AddElement(placeType);

            container.AddEntitySet("People", personType);
            container.AddEntitySet("Organizations", organizationType);
            container.AddEntitySet("Places", organizationType);

            return(edmModel);
        }