コード例 #1
0
        public void CreateODataComplexValue_Understands_IEdmComplexTypeObject()
        {
            // Arrange
            EdmComplexType complexEdmType = new EdmComplexType("NS", "ComplexType");

            complexEdmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32);
            IEdmComplexTypeReference edmTypeReference = new EdmComplexTypeReference(complexEdmType, isNullable: false);

            TypedEdmComplexObject      edmObject  = new TypedEdmComplexObject(new { Property = 42 }, edmTypeReference);
            ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(edmTypeReference, new DefaultODataSerializerProvider());

            // Act
            ODataComplexValue result = serializer.CreateODataComplexValue(edmObject, new ODataSerializerContext());

            // Assert
            Assert.Equal("Property", result.Properties.Single().Name);
            Assert.Equal(42, result.Properties.Single().Value);
        }
コード例 #2
0
        public void ParameterReaderShouldReadSingleDerivedComplexValue()
        {
            var complexType        = this.referencedModel.ComplexType("address").Property("StreetName", EdmPrimitiveTypeKind.String);
            var derivedComplexType = new EdmComplexType("TestModel", "derivedAddress", complexType, false);

            derivedComplexType.AddStructuralProperty("StreetNumber", EdmPrimitiveTypeKind.Int32, false);
            this.referencedModel.AddElement(derivedComplexType);

            this.action.AddParameter("address", new EdmComplexTypeReference(complexType, false));
            string payload = "{\"address\" : { \"StreetName\": \"Bla\", \"StreetNumber\" : 61, \"@odata.type\":\"TestModel.derivedAddress\" } }";

            var result = this.RunParameterReaderTest(payload);

            result.Entries.Should().HaveCount(1);
            result.Entries.Should().OnlyContain(keyValuePair => keyValuePair.Key.Equals("address"));

            result.Entries.SingleOrDefault().Value.Should().OnlyContain(item => item is ODataResource);;
        }
コード例 #3
0
        public void ParameterReaderShouldReadCollectionOfDerivedComplexValue()
        {
            var complexType        = this.referencedModel.ComplexType("address").Property("StreetName", EdmPrimitiveTypeKind.String);
            var derivedComplexType = new EdmComplexType("TestModel", "derivedAddress", complexType, false);

            derivedComplexType.AddStructuralProperty("StreetNumber", EdmPrimitiveTypeKind.Int32, false);
            this.referencedModel.AddElement(derivedComplexType);

            this.action.AddParameter("addresses", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, false)));
            string payload = "{\"addresses\" : [{ \"StreetName\": \"Bla\", \"StreetNumber\" : 61, \"@odata.type\":\"TestModel.derivedAddress\" }, { \"StreetName\": \"Bla2\" }]}";

            var result = this.RunParameterReaderTest(payload);

            result.Collections.Should().OnlyContain(keyValuePair => keyValuePair.Key.Equals("addresses"));
            var collectioItems = result.Collections.Single().Value.Items;

            collectioItems.Should().HaveCount(2);
            collectioItems.Should().OnlyContain(item => item is ODataComplexValue);
        }
コード例 #4
0
        private static IEdmModel GetEdmModel()
        {
            EdmModel model = new EdmModel();

            // ComplexType: Address
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(address);
            model.SetAnnotationValue(address, new ClrTypeAnnotation(typeof(MyAddress)));

            // ComplexType: CnAddress
            var cnAddress = new EdmComplexType("NS", "CnAddress", address);

            cnAddress.AddStructuralProperty("Zipcode", EdmPrimitiveTypeKind.String);
            model.AddElement(cnAddress);
            model.SetAnnotationValue(cnAddress, new ClrTypeAnnotation(typeof(CnMyAddress)));

            // EnumType: Color
            var color = new EdmEnumType("NS", "Color");

            model.AddElement(color);
            model.SetAnnotationValue(color, new ClrTypeAnnotation(typeof(MyColor)));

            // EntityType: MyCustomer
            var customer = new EdmEntityType("Microsoft.AspNetCore.OData.Tests.Edm", "MyCustomer");

            model.AddElement(customer);

            // Inheritance EntityType
            var baseEntity       = new EdmEntityType("NS", "BaseType");
            var derived1Entity   = new EdmEntityType("NS", "Derived1Type", baseEntity);
            var derived2Entity   = new EdmEntityType("NS", "Derived2Type", baseEntity);
            var subDerivedEntity = new EdmEntityType("NS", "SubDerivedType", derived1Entity);

            model.AddElements(new[] { baseEntity, derived1Entity, derived2Entity, subDerivedEntity });
            model.SetAnnotationValue(baseEntity, new ClrTypeAnnotation(typeof(BaseType)));
            model.SetAnnotationValue(derived1Entity, new ClrTypeAnnotation(typeof(Derived1Type)));
            model.SetAnnotationValue(derived2Entity, new ClrTypeAnnotation(typeof(Derived2Type)));
            model.SetAnnotationValue(subDerivedEntity, new ClrTypeAnnotation(typeof(SubDerivedType)));

            return(model);
        }
コード例 #5
0
        public static IEdmModel SimpleOpenTypeModel()
        {
            var model = new EdmModel();

            // Address is an open complex type
            var addressType = new EdmComplexType("Default", "Address", null, false, true);

            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // ZipCode is an open complex type also
            var zipCodeType = new EdmComplexType("Default", "ZipCode", null, false, true);

            zipCodeType.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Int32);
            model.AddElement(zipCodeType);

            // Enum type simpleEnum
            EdmEnumType simpleEnum = new EdmEnumType("Default", "SimpleEnum");

            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "First", new EdmIntegerConstant(0)));
            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Second", new EdmIntegerConstant(1)));
            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Third", new EdmIntegerConstant(2)));
            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Fourth", new EdmIntegerConstant(3)));
            model.AddElement(simpleEnum);

            // Customer is an open entity type
            var customerType = new EdmEntityType("Default", "Customer", null, false, true);

            customerType.AddKeys(customerType.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32));
            customerType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("Address", addressType.ToEdmTypeReference(false));
            model.AddElement(customerType);

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

            model.AddElement(container);

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

            container.AddElement(customers);
            return(model);
        }
コード例 #6
0
        public void ReadingPayloadOpenComplexTypeJsonLight()
        {
            EdmModel model = new EdmModel();

            EdmEntityType entityType = new EdmEntityType("NS", "Person");

            entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);

            EdmComplexType complexType = new EdmComplexType("NS", "OpenAddress", null, false, true);

            complexType.AddStructuralProperty("CountryRegion", EdmPrimitiveTypeKind.String, false);
            EdmComplexTypeReference complexTypeRef = new EdmComplexTypeReference(complexType, true);

            entityType.AddStructuralProperty("Address", complexTypeRef);

            model.AddElement(complexType);
            model.AddElement(entityType);

            EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer_sub");
            EdmEntitySet       entitySet = container.AddEntitySet("People", entityType);

            model.AddElement(container);

            const string payload =
                "{" +
                "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.People/$entity\"," +
                "\"@odata.id\":\"http://mytest\"," +
                "\"Id\":\"0\"," +
                "\"Address\":{\"CountryRegion\":\"China\",\"City\":\"Shanghai\"}" +
                "}";

            IEdmModel  mainModel = TestUtils.WrapReferencedModelsToMainModel("EntityNs", "MyContainer", model);
            ODataEntry entry     = null;

            this.ReadEntryPayload(mainModel, payload, entitySet, entityType, reader => { entry = entry ?? reader.Item as ODataEntry; });
            Assert.IsNotNull(entry, "entry shouldn't be null");

            var address = entry.Properties.FirstOrDefault(s => string.Equals(s.Name, "Address", StringComparison.OrdinalIgnoreCase)).Value as ODataComplexValue;

            address.Properties.FirstOrDefault(s => string.Equals(s.Name, "CountryRegion", StringComparison.OrdinalIgnoreCase)).Value.ShouldBeEquivalentTo("China", "value should be in correct type.");
            address.Properties.FirstOrDefault(s => string.Equals(s.Name, "City", StringComparison.OrdinalIgnoreCase)).Value.ShouldBeEquivalentTo("Shanghai", "value should be in correct type.");
        }
        public void ReadingNullValueForDeclaredComplexProperty()
        {
            EdmModel model = new EdmModel();

            EdmComplexType complexType = new EdmComplexType("NS", "Address");

            complexType.AddStructuralProperty("CountriesOrRegions", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true))));
            model.AddElement(complexType);

            EdmEntityType entityType = new EdmEntityType("NS", "Person");

            entityType.AddKeys(entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            entityType.AddStructuralProperty("Address", new EdmComplexTypeReference(complexType, true));
            model.AddElement(entityType);

            EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer");
            EdmEntitySet       entitySet = container.AddEntitySet("People", entityType);

            model.AddElement(container);

            const string payload =
                "{" +
                "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.People/$entity\"," +
                "\"@odata.id\":\"http://mytest\"," +
                "\"Id\":0," +
                "\"Address\":null" +
                "}";

            List <ODataItem> resources = new List <ODataItem>();

            this.ReadEntryPayload(model, payload, entitySet, entityType,
                                  reader =>
            {
                if (reader.State == ODataReaderState.ResourceEnd)
                {
                    resources.Add(reader.Item);
                }
            });

            Assert.Equal(2, resources.Count);
            Assert.Null(resources.First());
        }
コード例 #8
0
        public void WritingResourceValueWithPropertiesShouldWrite()
        {
            var complexType = new EdmComplexType("NS", "Address");

            complexType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(complexType);

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

            entityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            entityType.AddStructuralProperty("Location", new EdmComplexTypeReference(complexType, false));
            model.AddElement(entityType);

            settings.ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*");
            var result = this.SetupSerializerAndRunTest(serializer =>
            {
                var resourceValue = new ODataResourceValue
                {
                    TypeName   = "NS.Customer",
                    Properties = new []
                    {
                        new ODataProperty {
                            Name = "Name", Value = "MyName"
                        },
                        new ODataProperty {
                            Name = "Location", Value = new ODataResourceValue
                            {
                                TypeName   = "NS.Address",
                                Properties = new [] { new ODataProperty {
                                                          Name = "City", Value = "MyCity"
                                                      } }
                            }
                        }
                    }
                };

                var entityTypeRef = new EdmEntityTypeReference(entityType, false);
                serializer.WriteResourceValue(resourceValue, entityTypeRef, false, serializer.CreateDuplicatePropertyNameChecker());
            });

            Assert.Equal(@"{""Name"":""MyName"",""Location"":{""City"":""MyCity""}}", result);
        }
コード例 #9
0
        public void CreateSpatialSchemasReturnFullSpatialSchemasForModelWithEdmSpatialTypes()
        {
            // Arrange
            EdmModel       model   = new EdmModel();
            EdmComplexType complex = new EdmComplexType("NS", "Complex");

            complex.AddStructuralProperty("Location", EdmPrimitiveTypeKind.Geography);
            model.AddElement(complex);

            ODataContext context = new ODataContext(model);

            // Act
            var schemas = context.CreateSpatialSchemas();

            // Assert
            Assert.NotNull(schemas);
            Assert.NotEmpty(schemas);
            Assert.Equal(new string[]
            {
                "Edm.Geography",
                "Edm.GeographyPoint",
                "Edm.GeographyLineString",
                "Edm.GeographyPolygon",
                "Edm.GeographyMultiPoint",
                "Edm.GeographyMultiLineString",
                "Edm.GeographyMultiPolygon",
                "Edm.GeographyCollection",

                "Edm.Geometry",
                "Edm.GeometryPoint",
                "Edm.GeometryLineString",
                "Edm.GeometryPolygon",
                "Edm.GeometryMultiPoint",
                "Edm.GeometryMultiLineString",
                "Edm.GeometryMultiPolygon",
                "Edm.GeometryCollection",

                "GeoJSON.position"
            },
                         schemas.Select(s => s.Key));
        }
コード例 #10
0
ファイル: OpenEntityTypeTest.cs プロジェクト: rtapankm/WebApi
        private static IEdmModel GetUntypedEdmModel()
        {
            var model = new EdmModel();
            // complex type address
            EdmComplexType address = new EdmComplexType("NS", "Address", null, false, true);

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            // enum type color
            EdmEnumType color = new EdmEnumType("NS", "Color");

            color.AddMember(new EdmEnumMember(color, "Red", new EdmIntegerConstant(0)));
            model.AddElement(color);

            // entity type customer
            EdmEntityType customer = new EdmEntityType("NS", "UntypedSimpleOpenCustomer", null, false, true);

            customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32));
            customer.AddStructuralProperty("Color", new EdmEnumTypeReference(color, isNullable: true));
            model.AddElement(customer);

            EdmAction action = new EdmAction(
                "NS",
                "AddColor",
                null,
                isBound: true,
                entitySetPathExpression: null);

            action.AddParameter("bindingParameter", new EdmEntityTypeReference(customer, false));
            action.AddParameter("Color", new EdmEnumTypeReference(color, true));
            model.AddElement(action);

            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");

            container.AddEntitySet("UntypedSimpleOpenCustomers", customer);

            model.AddElement(container);
            return(model);
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: EricCote/WebApi2
        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);
        }
コード例 #12
0
        public void ParameterReaderShouldReadCollectionOfDerivedComplexValue()
        {
            var complexType        = this.referencedModel.ComplexType("address").Property("StreetName", EdmPrimitiveTypeKind.String);
            var derivedComplexType = new EdmComplexType("TestModel", "derivedAddress", complexType, false);

            derivedComplexType.AddStructuralProperty("StreetNumber", EdmPrimitiveTypeKind.Int32, false);
            this.referencedModel.AddElement(derivedComplexType);

            this.action.AddParameter("addresses", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, false)));
            string payload = "{\"addresses\" : [{ \"StreetName\": \"Bla\", \"StreetNumber\" : 61, \"@odata.type\":\"TestModel.derivedAddress\" }, { \"StreetName\": \"Bla2\" }]}";

            var result = this.RunParameterReaderTest(payload);

            var item = Assert.Single(result.Feeds);

            Assert.Equal("addresses", item.Key);

            var collectioItems = Assert.Single(result.Entries).Value;

            Assert.Equal(2, collectioItems.Count());
            Assert.Equal(2, collectioItems.OfType <ODataResource>().Count());
        }
コード例 #13
0
        public static IEdmModel MultipleSchemasWithDifferentNamespacesEdm()
        {
            var namespaces = new string[]
            {
                "FindMethodsTestModelBuilder.MultipleSchemasWithDifferentNamespaces.first",
                "FindMethodsTestModelBuilder.MultipleSchemasWithDifferentNamespaces.second"
            };

            var model = new EdmModel();

            foreach (var namespaceName in namespaces)
            {
                var entityType1 = new EdmEntityType(namespaceName, "validEntityType1");
                entityType1.AddKeys(entityType1.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
                var entityType2 = new EdmEntityType(namespaceName, "VALIDeNTITYtYPE2");
                entityType2.AddKeys(entityType2.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
                var entityType3 = new EdmEntityType(namespaceName, "VALIDeNTITYtYPE3");
                entityType3.AddKeys(entityType3.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));

                entityType1.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                    Name = "Mumble", Target = entityType2, TargetMultiplicity = EdmMultiplicity.Many
                });

                var complexType = new EdmComplexType(namespaceName, "ValidNameComplexType1");
                complexType.AddStructuralProperty("aPropertyOne", new EdmComplexTypeReference(complexType, false));
                model.AddElements(new IEdmSchemaElement[] { entityType1, entityType2, entityType3, complexType });

                var function1 = new EdmFunction(namespaceName, "ValidFunction1", EdmCoreModel.Instance.GetSingle(false));
                var function2 = new EdmFunction(namespaceName, "ValidFunction1", EdmCoreModel.Instance.GetSingle(false));
                function2.AddParameter("param1", new EdmEntityTypeReference(entityType1, false));
                var function3 = new EdmFunction(namespaceName, "ValidFunction1", EdmCoreModel.Instance.GetSingle(false));
                function3.AddParameter("param1", EdmCoreModel.Instance.GetSingle(false));

                model.AddElements(new IEdmSchemaElement[] { function1, function2, function3 });
            }

            return(model);
        }
コード例 #14
0
        public void WritingCollectionResourceValueWithPropertiesShouldWrite()
        {
            var complexType = new EdmComplexType("NS", "Address");

            complexType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(complexType);

            settings.ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*");
            var result = this.SetupSerializerAndRunTest(serializer =>
            {
                var resourceValue1 = new ODataResourceValue
                {
                    TypeName   = "NS.Address",
                    Properties = new[] { new ODataProperty {
                                             Name = "City", Value = "MyCity1"
                                         } }
                };

                var resourceValue2 = new ODataResourceValue
                {
                    TypeName   = "NS.Address",
                    Properties = new[] { new ODataProperty {
                                             Name = "City", Value = "MyCity2"
                                         } }
                };

                var collectionValue = new ODataCollectionValue
                {
                    TypeName = "Collection(NS.Address)",
                    Items    = new[] { resourceValue1, resourceValue2 }
                };

                var collectionTypeRef = new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(complexType, false)));
                serializer.WriteCollectionValue(collectionValue, collectionTypeRef, null, false, false, false);
            });

            Assert.Equal(@"[{""City"":""MyCity1""},{""City"":""MyCity2""}]", result);
        }
コード例 #15
0
        private static IEdmModel GetEdmModel()
        {
            EdmModel model = new EdmModel();

            // complex type address
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            EdmComplexType vipAddress = new EdmComplexType("NS", "VipAddress");

            vipAddress.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(vipAddress);

            EdmEntityType company = new EdmEntityType("NS", "Company");

            company.AddKeys(company.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            company.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Int32);
            company.AddStructuralProperty("Location", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(company);
            return(model);
        }
コード例 #16
0
        public static IEdmModel IsTypeResultTrueEvaluationModel()
        {
            var model = new EdmModel();

            var booleanFlag = new EdmComplexType("NS", "BooleanFlag");
            var flag        = booleanFlag.AddStructuralProperty("Flag", EdmCoreModel.Instance.GetBoolean(true));

            model.AddElement(booleanFlag);

            var booleanFlagTerm = new EdmTerm("NS", "BooleanFlagTerm", new EdmComplexTypeReference(booleanFlag, true));

            model.AddElement(booleanFlagTerm);

            var valueAnnotation = new EdmVocabularyAnnotation(
                booleanFlag,
                booleanFlagTerm,
                new EdmRecordExpression(
                    new EdmPropertyConstructor(flag.Name, new EdmIsTypeExpression(new EdmStringConstant("foo"), EdmCoreModel.Instance.GetString(true)))));

            valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine);
            model.AddVocabularyAnnotation(valueAnnotation);

            return(model);
        }
コード例 #17
0
        public UriParameterReaderIntegrationTests()
        {
            model = new EdmModel();

            cityT = new EdmComplexType(ns, "City");
            cityT.AddStructuralProperty("CityName", EdmPrimitiveTypeKind.String, false);
            cityT.AddStructuralProperty("AreaCode", EdmPrimitiveTypeKind.String, true);
            model.AddElement(cityT);

            addressT = new EdmComplexType(ns, "Address");
            addressT.AddStructuralProperty("City", new EdmComplexTypeReference(cityT, true));
            model.AddElement(addressT);

            companyAddressT = new EdmComplexType(ns, "CompanyAddress", addressT, false);
            companyAddressT.AddStructuralProperty("CompanyName", EdmPrimitiveTypeKind.String);
            model.AddElement(companyAddressT);

            otherInfoT = new EdmComplexType(ns, "OtherInfo", null, false, true);
            otherInfoT.AddStructuralProperty("Hight", EdmPrimitiveTypeKind.Int32);
            model.AddElement(otherInfoT);

            personT = new EdmEntityType("NS", "Person", null, false, true);
            var Id = personT.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));

            personT.AddKeys(Id);
            personT.AddStructuralProperty("Address", new EdmComplexTypeReference(addressT, false));
            personT.AddStructuralProperty("CompanyAddress", new EdmComplexTypeReference(companyAddressT, true));
            billingAddressesT = new EdmCollectionType(new EdmComplexTypeReference(addressT, false));
            personT.AddStructuralProperty("BillingAddresses", new EdmCollectionTypeReference(billingAddressesT));
            model.AddElement(personT);

            EdmEntityContainer container = new EdmEntityContainer(ns, "Container");

            peopleSet = new EdmEntitySet(container, "people", personT);
            model.AddElement(container);
        }
コード例 #18
0
        public void ExtensionSetInternalAnnotation_Validation()
        {
            var model          = new EdmModel();
            var fredFlintstone = new EdmComplexType("Flintstones", "Fred");

            fredFlintstone.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
            model.AddElement(fredFlintstone);

            foreach (IEdmElement annotatable in model.SchemaElements)
            {
                // set
                model.SetAnnotationValue(annotatable, "foo");
                model.SetAnnotationValue(annotatable, new MyQualifiedName("foo", "bar"));
                model.SetAnnotationValue <IEdmType>(annotatable, new EdmComplexType("foo", "foo", null, false));
                model.SetAnnotationValue <Dictionary <string, int> >(annotatable, new Dictionary <string, int>());

                Assert.AreEqual(4, model.DirectValueAnnotations(annotatable).Count(), "Wrong # of Annotations on {0}.", annotatable);
            }

            IEnumerable <EdmError> errors;

            model.Validate(out errors);
            Assert.AreEqual(0, errors.Count(), "Model expected to be valid.");
        }
コード例 #19
0
        public ODataReaderDerivedTypeConstraintTests()
        {
            // Create the basic model
            edmModel = new EdmModel();

            // Entity Type
            edmCustomerType = new EdmEntityType("NS", "Customer");
            edmCustomerType.AddKeys(edmCustomerType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));

            edmVipCustomerType = new EdmEntityType("NS", "VipCustomer", edmCustomerType);
            edmVipCustomerType.AddStructuralProperty("Vip", EdmPrimitiveTypeKind.String);

            edmNormalCustomerType = new EdmEntityType("NS", "NormalCustomer", edmCustomerType);
            edmNormalCustomerType.AddStructuralProperty("Email", EdmPrimitiveTypeKind.String);

            edmModel.AddElements(new[] { edmCustomerType, edmVipCustomerType, edmNormalCustomerType });

            // Complex type
            edmAddressType = new EdmComplexType("NS", "Address");
            edmAddressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);

            edmUsAddressType = new EdmComplexType("NS", "UsAddress", edmAddressType);
            edmUsAddressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.Int32);

            edmCnAddressType = new EdmComplexType("NS", "CnAddress", edmAddressType);
            edmCnAddressType.AddStructuralProperty("PostCode", EdmPrimitiveTypeKind.String);

            edmModel.AddElements(new[] { edmAddressType, edmUsAddressType, edmCnAddressType });

            // EntityContainer
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");

            edmMe        = container.AddSingleton("Me", edmCustomerType);
            edmCustomers = container.AddEntitySet("Customers", edmCustomerType);
            edmModel.AddElement(container);
        }
コード例 #20
0
        private IEdmModel GetModel()
        {
            if (myModel == null)
            {
                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 orderType = new EdmEntityType("MyNS", "Order");
                orderType.AddKeys(orderType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
                orderType.AddStructuralProperty("ShippingAddress", shippingAddressReference);
                myModel.AddElement(orderType);

                EdmEntityType customer = new EdmEntityType("MyNS", "Customer");
                customer.AddStructuralProperty("ContactName", EdmPrimitiveTypeKind.String);
                var customerId = customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);
                customer.AddKeys(customerId);
                customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                {
                    Name               = "Orders",
                    Target             = orderType,
                    TargetMultiplicity = EdmMultiplicity.Many,
                });
                myModel.AddElement(customer);

                var productType = new EdmEntityType("MyNS", "Product");
                var productId   = productType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);
                productType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
                productType.AddKeys(productId);
                myModel.AddElement(productType);

                var physicalProductType = new EdmEntityType("MyNS", "PhysicalProduct", productType);
                physicalProductType.AddStructuralProperty("Material", EdmPrimitiveTypeKind.String);
                myModel.AddElement(physicalProductType);

                var productDetailType = new EdmEntityType("MyNS", "ProductDetail");
                var productDetailId   = productDetailType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);
                productDetailType.AddStructuralProperty("Detail", EdmPrimitiveTypeKind.String);
                productDetailType.AddKeys(productDetailId);
                myModel.AddElement(productDetailType);

                var productDetailItemType = new EdmEntityType("MyNS", "ProductDetailItem");
                var productDetailItemId   = productDetailItemType.AddStructuralProperty("ItemId", EdmPrimitiveTypeKind.Int32);
                productDetailItemType.AddStructuralProperty("Description", EdmPrimitiveTypeKind.String);
                productDetailItemType.AddKeys(productDetailItemId);
                myModel.AddElement(productDetailItemType);

                productType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                {
                    Name               = "Details",
                    Target             = productDetailType,
                    TargetMultiplicity = EdmMultiplicity.Many,
                    ContainsTarget     = true,
                });
                productDetailType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                {
                    Name               = "Items",
                    Target             = productDetailItemType,
                    TargetMultiplicity = EdmMultiplicity.Many,
                    ContainsTarget     = true,
                });

                var favouriteProducts = customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                {
                    Name               = "FavouriteProducts",
                    Target             = productType,
                    TargetMultiplicity = EdmMultiplicity.Many,
                });
                var productBeingViewed = customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                {
                    Name               = "ProductBeingViewed",
                    Target             = productType,
                    TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                });

                EdmEntityContainer container = new EdmEntityContainer("MyNS", "Example30");
                var products  = container.AddEntitySet("Products", productType);
                var customers = container.AddEntitySet("Customers", customer);
                customers.AddNavigationTarget(favouriteProducts, products);
                customers.AddNavigationTarget(productBeingViewed, products);
                container.AddEntitySet("Orders", orderType);
                myModel.AddElement(container);
            }
            return(myModel);
        }
コード例 #21
0
        public CustomersModelWithInheritance()
        {
            EdmModel model = new EdmModel();

            // complex type address
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

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

            customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true));
            customer.AddStructuralProperty("City", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, true),
                                           defaultValue: null, concurrencyMode: EdmConcurrencyMode.Fixed);
            model.AddElement(customer);

            // derived entity type special customer
            EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer);

            specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialCustomer);

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

            order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32);
            model.AddElement(order);

            // derived entity type special order
            EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order);

            specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialOrder);

            // entity sets
            EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance");

            model.AddElement(container);
            EdmEntitySet customers = container.AddEntitySet("Customers", customer);
            EdmEntitySet orders    = container.AddEntitySet("Orders", order);

            // actions
            EdmFunctionImport upgradeCustomer = container.AddFunctionImport(
                "upgrade", returnType: null, entitySet: new EdmEntitySetReferenceExpression(customers), sideEffecting: true, composable: false, bindable: true);

            upgradeCustomer.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            EdmFunctionImport upgradeSpecialCustomer = container.AddFunctionImport(
                "specialUpgrade", returnType: null, entitySet: new EdmEntitySetReferenceExpression(customers), sideEffecting: true, composable: false, bindable: true);

            upgradeSpecialCustomer.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));

            // navigation properties
            customers.AddNavigationTarget(
                customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "Orders",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = order
            }),
                orders);
            orders.AddNavigationTarget(
                order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "Customer",
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                Target             = customer
            }),
                customers);

            // navigation properties on derived types.
            customers.AddNavigationTarget(
                specialCustomer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "SpecialOrders",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = order
            }),
                orders);
            orders.AddNavigationTarget(
                specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "SpecialCustomer",
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                Target             = customer
            }),
                customers);
            model.SetAnnotationValue <BindableProcedureFinder>(model, new BindableProcedureFinder(model));

            // set properties
            Model                  = model;
            Container              = container;
            Customer               = customer;
            Order                  = order;
            Address                = address;
            SpecialCustomer        = specialCustomer;
            SpecialOrder           = specialOrder;
            Orders                 = orders;
            Customers              = customers;
            UpgradeCustomer        = upgradeCustomer;
            UpgradeSpecialCustomer = upgradeSpecialCustomer;
        }
コード例 #22
0
        public void WriteNavigationPropertyInComplexType()
        {
            var model = new EdmModel();

            var person   = new EdmEntityType("DefaultNs", "Person");
            var entityId = person.AddStructuralProperty("UserName", EdmCoreModel.Instance.GetString(false));

            person.AddKeys(entityId);

            var city   = new EdmEntityType("DefaultNs", "City");
            var cityId = city.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));

            city.AddKeys(cityId);

            var countryOrRegion = new EdmEntityType("DefaultNs", "CountryOrRegion");
            var countryId       = countryOrRegion.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));

            countryOrRegion.AddKeys(countryId);

            var complex = new EdmComplexType("DefaultNs", "Address");

            complex.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            var navP = complex.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "City",
                Target             = city,
                TargetMultiplicity = EdmMultiplicity.One,
            });

            var derivedComplex = new EdmComplexType("DefaultNs", "WorkAddress", complex);
            var navP2          = derivedComplex.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "CountryOrRegion",
                Target             = countryOrRegion,
                TargetMultiplicity = EdmMultiplicity.One,
            });

            person.AddStructuralProperty("HomeAddress", new EdmComplexTypeReference(complex, false));
            person.AddStructuralProperty("WorkAddress", new EdmComplexTypeReference(complex, false));
            person.AddStructuralProperty("Addresses", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(complex, false))));

            model.AddElement(person);
            model.AddElement(city);
            model.AddElement(countryOrRegion);
            model.AddElement(complex);
            model.AddElement(derivedComplex);

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

            model.AddElement(entityContainer);
            EdmEntitySet people             = new EdmEntitySet(entityContainer, "People", person);
            EdmEntitySet cities             = new EdmEntitySet(entityContainer, "City", city);
            EdmEntitySet countriesOrRegions = new EdmEntitySet(entityContainer, "CountryOrRegion", countryOrRegion);

            people.AddNavigationTarget(navP, cities, new EdmPathExpression("HomeAddress/City"));
            people.AddNavigationTarget(navP, cities, new EdmPathExpression("Addresses/City"));
            people.AddNavigationTarget(navP2, countriesOrRegions, new EdmPathExpression("WorkAddress/DefaultNs.WorkAddress/CountryOrRegion"));
            entityContainer.AddElement(people);
            entityContainer.AddElement(cities);
            entityContainer.AddElement(countriesOrRegions);

            IEnumerable <EdmError> actualErrors = null;

            model.Validate(out actualErrors);
            Assert.Equal(actualErrors.Count(), 0);

            string actual = GetCsdl(model, CsdlTarget.OData);

            string expected =
                "<?xml version=\"1.0\" encoding=\"utf-16\"?><edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">" +
                "<edmx:DataServices>" +
                "<Schema Namespace=\"DefaultNs\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">" +
                "<EntityType Name=\"Person\">" +
                "<Key><PropertyRef Name=\"UserName\" /></Key>" +
                "<Property Name=\"UserName\" Type=\"Edm.String\" Nullable=\"false\" />" +
                "<Property Name=\"HomeAddress\" Type=\"DefaultNs.Address\" Nullable=\"false\" />" +
                "<Property Name=\"WorkAddress\" Type=\"DefaultNs.Address\" Nullable=\"false\" />" +
                "<Property Name=\"Addresses\" Type=\"Collection(DefaultNs.Address)\" Nullable=\"false\" />" +
                "</EntityType>" +
                "<EntityType Name=\"City\">" +
                "<Key><PropertyRef Name=\"Name\" /></Key>" +
                "<Property Name=\"Name\" Type=\"Edm.String\" Nullable=\"false\" />" +
                "</EntityType>" +
                "<EntityType Name=\"CountryOrRegion\">" +
                "<Key><PropertyRef Name=\"Name\" /></Key>" +
                "<Property Name=\"Name\" Type=\"Edm.String\" Nullable=\"false\" />" +
                "</EntityType>" +
                "<ComplexType Name=\"Address\">" +
                "<Property Name=\"Id\" Type=\"Edm.Int32\" Nullable=\"false\" />" +
                "<NavigationProperty Name=\"City\" Type=\"DefaultNs.City\" Nullable=\"false\" />" +
                "</ComplexType>" +
                "<ComplexType Name=\"WorkAddress\" BaseType=\"DefaultNs.Address\">" +
                "<NavigationProperty Name=\"CountryOrRegion\" Type=\"DefaultNs.CountryOrRegion\" Nullable=\"false\" /><" +
                "/ComplexType>" +
                "<EntityContainer Name=\"Container\">" +
                "<EntitySet Name=\"People\" EntityType=\"DefaultNs.Person\">" +
                "<NavigationPropertyBinding Path=\"HomeAddress/City\" Target=\"City\" />" +
                "<NavigationPropertyBinding Path=\"Addresses/City\" Target=\"City\" />" +
                "<NavigationPropertyBinding Path=\"WorkAddress/DefaultNs.WorkAddress/CountryOrRegion\" Target=\"CountryOrRegion\" />" +
                "</EntitySet>" +
                "<EntitySet Name=\"City\" EntityType=\"DefaultNs.City\" />" +
                "<EntitySet Name=\"CountryOrRegion\" EntityType=\"DefaultNs.CountryOrRegion\" />" +
                "</EntityContainer></Schema>" +
                "</edmx:DataServices>" +
                "</edmx:Edmx>";

            Assert.Equal(expected, actual);
        }
コード例 #23
0
ファイル: MultiBindingTests.cs プロジェクト: zhonli/odata.net
        private static IEdmModel GetModel()
        {
            var model = new EdmModel();

            var entityType = new EdmEntityType("NS", "EntityType");
            var id         = entityType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            entityType.AddKeys(id);

            var derivedEntityType = new EdmEntityType("NS", "DerivedEntityType", entityType);

            var containedEntityType = new EdmEntityType("NS", "ContainedEntityType");
            var containedId         = containedEntityType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            containedEntityType.AddKeys(containedId);

            var containedNav1 = entityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "ContainedNav1",
                Target             = containedEntityType,
                TargetMultiplicity = EdmMultiplicity.One,
                ContainsTarget     = true
            });

            var containedNav2 = entityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "ContainedNav2",
                Target             = containedEntityType,
                TargetMultiplicity = EdmMultiplicity.One,
                ContainsTarget     = true
            });

            var navEntityType = new EdmEntityType("NS", "NavEntityType");
            var navEntityId   = navEntityType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            navEntityType.AddKeys(navEntityId);

            var nestedNavEntityType = new EdmEntityType("NS", "NestedNavEntityType");
            var nestedId            = nestedNavEntityType.AddStructuralProperty("NestedId", EdmCoreModel.Instance.GetString(false));

            nestedNavEntityType.AddKeys(nestedId);

            var nestNav = navEntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "NavNested",
                Target             = nestedNavEntityType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var complex = new EdmComplexType("NS", "ComplexType");

            complex.AddStructuralProperty("Prop1", EdmCoreModel.Instance.GetString(false));
            complex.AddStructuralProperty("CollectionComplexProp", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(complex, false))));

            var derivedComplex = new EdmComplexType("NS", "DerivedComplexType", complex);

            derivedComplex.AddStructuralProperty("DerivedProp", EdmCoreModel.Instance.GetString(false));

            var derivedNav = derivedEntityType.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "NavOnDerived",
                Target             = navEntityType,
                TargetMultiplicity = EdmMultiplicity.One,
            });

            var complxNavP = complex.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "CollectionOfNavOnComplex",
                Target             = navEntityType,
                TargetMultiplicity = EdmMultiplicity.Many,
            });

            entityType.AddStructuralProperty("complexProp1", new EdmComplexTypeReference(complex, false));
            entityType.AddStructuralProperty("complexProp2", new EdmComplexTypeReference(complex, false));
            entityType.AddStructuralProperty("collectionComplex", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(complex, false))));

            var navOnContained = containedEntityType.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "NavOnContained",
                Target             = navEntityType,
                TargetMultiplicity = EdmMultiplicity.One,
            });

            var manyNavOnContained = containedEntityType.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "ManyNavOnContained",
                Target             = navEntityType,
                TargetMultiplicity = EdmMultiplicity.Many,
            });

            model.AddElement(entityType);
            model.AddElement(derivedEntityType);
            model.AddElement(containedEntityType);
            model.AddElement(navEntityType);
            model.AddElement(nestedNavEntityType);
            model.AddElement(complex);
            model.AddElement(derivedComplex);

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

            model.AddElement(entityContainer);
            var entitySet        = new EdmEntitySet(entityContainer, "EntitySet", entityType);
            var navEntitySet1    = new EdmEntitySet(entityContainer, "NavEntitySet1", navEntityType);
            var navEntitySet2    = new EdmEntitySet(entityContainer, "NavEntitySet2", navEntityType);
            var nestNavEntitySet = new EdmEntitySet(entityContainer, "NestedEntitySet", nestedNavEntityType);

            navEntitySet1.AddNavigationTarget(nestNav, nestNavEntitySet);
            navEntitySet2.AddNavigationTarget(nestNav, nestNavEntitySet);
            entitySet.AddNavigationTarget(derivedNav, navEntitySet1, new EdmPathExpression("NS.DerivedEntityType/NavOnDerived"));
            entitySet.AddNavigationTarget(complxNavP, navEntitySet1, new EdmPathExpression("complexProp1/CollectionOfNavOnComplex"));
            entitySet.AddNavigationTarget(complxNavP, navEntitySet2, new EdmPathExpression("complexProp2/CollectionOfNavOnComplex"));
            entitySet.AddNavigationTarget(navOnContained, navEntitySet1, new EdmPathExpression("ContainedNav1/NavOnContained"));
            entitySet.AddNavigationTarget(navOnContained, navEntitySet2, new EdmPathExpression("ContainedNav2/NavOnContained"));
            entitySet.AddNavigationTarget(manyNavOnContained, navEntitySet1, new EdmPathExpression("ContainedNav1/ManyNavOnContained"));
            entitySet.AddNavigationTarget(manyNavOnContained, navEntitySet2, new EdmPathExpression("ContainedNav2/ManyNavOnContained"));
            entitySet.AddNavigationTarget(complxNavP, navEntitySet2, new EdmPathExpression("collectionComplex/CollectionOfNavOnComplex"));
            entitySet.AddNavigationTarget(complxNavP, navEntitySet1, new EdmPathExpression("collectionComplex/CollectionComplexProp/CollectionOfNavOnComplex"));
            entityContainer.AddElement(entitySet);
            entityContainer.AddElement(navEntitySet1);
            entityContainer.AddElement(navEntitySet2);
            entityContainer.AddElement(nestNavEntitySet);

            return(model);
        }
コード例 #24
0
        public void WriteCollectionOfNavigationOnComplex()
        {
            var model = new EdmModel();

            var entity   = new EdmEntityType("DefaultNs", "EntityType");
            var entityId = entity.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            entity.AddKeys(entityId);

            var navEntity   = new EdmEntityType("DefaultNs", "NavEntityType");
            var navEntityId = navEntity.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            navEntity.AddKeys(navEntityId);

            var complex = new EdmComplexType("DefaultNs", "ComplexType");

            complex.AddStructuralProperty("Prop1", EdmCoreModel.Instance.GetInt32(false));

            var navP = complex.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "CollectionOfNav",
                Target             = navEntity,
                TargetMultiplicity = EdmMultiplicity.Many,
            });

            entity.AddStructuralProperty("Complex", new EdmComplexTypeReference(complex, false));

            model.AddElement(entity);
            model.AddElement(navEntity);
            model.AddElement(complex);

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

            model.AddElement(entityContainer);
            EdmEntitySet entites     = new EdmEntitySet(entityContainer, "Entities", entity);
            EdmEntitySet navEntities = new EdmEntitySet(entityContainer, "NavEntities", navEntity);

            entites.AddNavigationTarget(navP, navEntities, new EdmPathExpression("Complex/CollectionOfNav"));
            entityContainer.AddElement(entites);
            entityContainer.AddElement(navEntities);

            string actual = GetCsdl(model, CsdlTarget.OData);

            string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">" +
                              "<edmx:DataServices><Schema Namespace=\"DefaultNs\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">" +
                              "<EntityType Name=\"EntityType\">" +
                              "<Key><PropertyRef Name=\"ID\" /></Key>" +
                              "<Property Name=\"ID\" Type=\"Edm.String\" Nullable=\"false\" />" +
                              "<Property Name=\"Complex\" Type=\"DefaultNs.ComplexType\" Nullable=\"false\" />" +
                              "</EntityType>" +
                              "<EntityType Name=\"NavEntityType\">" +
                              "<Key><PropertyRef Name=\"ID\" /></Key>" +
                              "<Property Name=\"ID\" Type=\"Edm.String\" Nullable=\"false\" />" +
                              "</EntityType>" +
                              "<ComplexType Name=\"ComplexType\">" +
                              "<Property Name=\"Prop1\" Type=\"Edm.Int32\" Nullable=\"false\" />" +
                              "<NavigationProperty Name=\"CollectionOfNav\" Type=\"Collection(DefaultNs.NavEntityType)\" />" +
                              "</ComplexType>" +
                              "<EntityContainer Name=\"Container\">" +
                              "<EntitySet Name=\"Entities\" EntityType=\"DefaultNs.EntityType\">" +
                              "<NavigationPropertyBinding Path=\"Complex/CollectionOfNav\" Target=\"NavEntities\" />" +
                              "</EntitySet>" +
                              "<EntitySet Name=\"NavEntities\" EntityType=\"DefaultNs.NavEntityType\" />" +
                              "</EntityContainer>" +
                              "</Schema>" +
                              "</edmx:DataServices>" +
                              "</edmx:Edmx>";

            Assert.Equal(expected, actual);
        }
コード例 #25
0
        public void ContainedUnderComplexTest()
        {
            var model = new EdmModel();

            var entity   = new EdmEntityType("NS", "EntityType");
            var entityId = entity.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            entity.AddKeys(entityId);

            var containedEntity   = new EdmEntityType("NS", "ContainedEntityType");
            var containedEntityId = containedEntity.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            containedEntity.AddKeys(containedEntityId);

            var complex = new EdmComplexType("NS", "ComplexType");

            complex.AddStructuralProperty("Prop1", EdmCoreModel.Instance.GetInt32(false));

            var containedUnderComplex = complex.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "ContainedUnderComplex",
                Target             = containedEntity,
                TargetMultiplicity = EdmMultiplicity.Many,
                ContainsTarget     = true
            });

            var navUnderContained = containedEntity.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo()
            {
                Name               = "NavUnderContained",
                Target             = entity,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            entity.AddStructuralProperty("Complex", new EdmComplexTypeReference(complex, false));

            model.AddElement(entity);
            model.AddElement(containedEntity);
            model.AddElement(complex);

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

            model.AddElement(entityContainer);
            EdmEntitySet entites1 = new EdmEntitySet(entityContainer, "Entities1", entity);
            EdmEntitySet entites2 = new EdmEntitySet(entityContainer, "Entities2", entity);

            entites1.AddNavigationTarget(navUnderContained, entites2,
                                         new EdmPathExpression("Complex/ContainedUnderComplex/NavUnderContained"));
            entityContainer.AddElement(entites1);
            entityContainer.AddElement(entites2);

            string actual = GetCsdl(model, CsdlTarget.OData);

            var entitySet1         = model.EntityContainer.FindEntitySet("Entities1");
            var entitySet2         = model.EntityContainer.FindEntitySet("Entities2");
            var containedEntitySet = entitySet1.FindNavigationTarget(containedUnderComplex);

            Assert.Equal(containedEntitySet.Name, "ContainedUnderComplex");
            var entitySetUnderContained = containedEntitySet.FindNavigationTarget(navUnderContained,
                                                                                  new EdmPathExpression("Complex/ContainedUnderComplex/NavUnderContained"));

            Assert.Equal(entitySetUnderContained, entitySet2);
        }
コード例 #26
0
        public static IEdmModel SimpleCustomerOrderModel()
        {
            var model        = new EdmModel();
            var customerType = new EdmEntityType("Default", "Customer");

            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                EdmPrimitiveTypeKind.String,
                isNullable: true);

            customerType.AddStructuralProperty(
                "City",
                primitiveTypeReference,
                defaultValue: null,
                concurrencyMode: EdmConcurrencyMode.Fixed);
            model.AddElement(customerType);

            var specialCustomerType = new EdmEntityType("Default", "SpecialCustomer", customerType);

            model.AddElement(specialCustomerType);

            var orderType = new EdmEntityType("Default", "Order");

            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            orderType.AddStructuralProperty("Shipment", EdmPrimitiveTypeKind.String);
            model.AddElement(orderType);

            var specialOrderType = new EdmEntityType("Default", "SpecialOrder", orderType);

            model.AddElement(specialOrderType);

            var addressType = new EdmComplexType("Default", "Address");

            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // Add navigations
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many
            });
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Customer", Target = customerType, TargetMultiplicity = EdmMultiplicity.One
            });
            specialCustomerType.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name               = "SpecialOrders",
                Target             = specialOrderType,
                TargetMultiplicity = EdmMultiplicity.Many
            });
            orderType.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name               = "SpecialCustomer",
                Target             = specialCustomerType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            // Add Entity set
            var container   = new EdmEntityContainer("Default", "Container");
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet    = container.AddEntitySet("Orders", orderType);

            customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet);
            customerSet.AddNavigationTarget(
                specialCustomerType.NavigationProperties().Single(np => np.Name == "SpecialOrders"),
                orderSet);
            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet);
            orderSet.AddNavigationTarget(
                specialOrderType.NavigationProperties().Single(np => np.Name == "SpecialCustomer"),
                customerSet);

            NavigationSourceLinkBuilderAnnotation linkAnnotation = new MockNavigationSourceLinkBuilderAnnotation();

            model.SetNavigationSourceLinkBuilder(customerSet, linkAnnotation);
            model.SetNavigationSourceLinkBuilder(orderSet, linkAnnotation);

            model.AddElement(container);
            return(model);
        }
コード例 #27
0
        /// <summary>
        /// Creates a test model to test our conversion of OData instances into EDM values.
        /// </summary>
        /// <returns>Returns a model suitable for testing EDM values over OData instances.</returns>
        public static IEdmModel BuildEdmValueModel()
        {
            EdmModel model       = new EdmModel();
            var      complexType = new EdmComplexType(DefaultNamespaceName, "ComplexType");

            complexType.AddStructuralProperty("IntProp", Int32TypeRef);
            complexType.AddStructuralProperty("StringProp", EdmCoreModel.Instance.GetString(isNullable: false));
            complexType.AddStructuralProperty("ComplexProp", new EdmComplexTypeReference(complexType, isNullable: true));
            model.AddElement(complexType);

            #region Entity types
            var entityContainer = new EdmEntityContainer(DefaultNamespaceName, "TestContainer");
            model.AddElement(entityContainer);

            // Entity type with a single primitive property
            var singlePrimitivePropertyEntityType = new EdmEntityType(DefaultNamespaceName, "SinglePrimitivePropertyEntityType");
            singlePrimitivePropertyEntityType.AddKeys(singlePrimitivePropertyEntityType.AddStructuralProperty("ID", Int32TypeRef));
            singlePrimitivePropertyEntityType.AddStructuralProperty("Int32Prop", Int32NullableTypeRef);
            entityContainer.AddEntitySet("SinglePrimitivePropertyEntityType", singlePrimitivePropertyEntityType);
            model.AddElement(singlePrimitivePropertyEntityType);

            // Entity type with all primitive properties
            var allPrimitivePropertiesEntityType = new EdmEntityType(DefaultNamespaceName, "AllPrimitivePropertiesEntityType");
            allPrimitivePropertiesEntityType.AddKeys(allPrimitivePropertiesEntityType.AddStructuralProperty("ID", Int32TypeRef));
            allPrimitivePropertiesEntityType.AddStructuralProperty("BoolProp", EdmPrimitiveTypeKind.Boolean, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("Int16Prop", EdmPrimitiveTypeKind.Int16, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("Int32Prop", EdmPrimitiveTypeKind.Int32, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("Int64Prop", EdmPrimitiveTypeKind.Int64, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("ByteProp", EdmPrimitiveTypeKind.Byte, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("SByteProp", EdmPrimitiveTypeKind.SByte, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("SingleProp", EdmPrimitiveTypeKind.Single, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("DoubleProp", EdmPrimitiveTypeKind.Double, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("DecimalProp", EdmPrimitiveTypeKind.Decimal, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("DateTimeOffsetProp", EdmPrimitiveTypeKind.DateTimeOffset, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("TimeProp", EdmPrimitiveTypeKind.Duration, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("GuidProp", EdmPrimitiveTypeKind.Guid, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("StringProp", EdmPrimitiveTypeKind.String, isNullable: false);
            allPrimitivePropertiesEntityType.AddStructuralProperty("BinaryProp", EdmPrimitiveTypeKind.Binary, isNullable: false);
            entityContainer.AddEntitySet("AllPrimitivePropertiesEntityType", allPrimitivePropertiesEntityType);
            model.AddElement(allPrimitivePropertiesEntityType);

            // Entity type with a single complex property
            var singleComplexPropertyEntityType = new EdmEntityType(DefaultNamespaceName, "SingleComplexPropertyEntityType");
            singleComplexPropertyEntityType.AddKeys(singleComplexPropertyEntityType.AddStructuralProperty("ID", Int32TypeRef));
            singleComplexPropertyEntityType.AddStructuralProperty("ComplexProp", new EdmComplexTypeReference(complexType, isNullable: true));
            entityContainer.AddEntitySet("SingleComplexPropertyEntityType", singleComplexPropertyEntityType);
            model.AddElement(singleComplexPropertyEntityType);

            // Entity type with a single primitive collection property
            var singlePrimitiveCollectionPropertyEntityType = new EdmEntityType(DefaultNamespaceName, "SinglePrimitiveCollectionPropertyEntityType");
            singlePrimitiveCollectionPropertyEntityType.AddKeys(singlePrimitiveCollectionPropertyEntityType.AddStructuralProperty("ID", Int32TypeRef));
            singlePrimitiveCollectionPropertyEntityType.AddStructuralProperty("PrimitiveCollectionProp", EdmCoreModel.GetCollection(Int32TypeRef));
            entityContainer.AddEntitySet("SinglePrimitiveCollectionPropertyEntityType", singlePrimitiveCollectionPropertyEntityType);
            model.AddElement(singlePrimitiveCollectionPropertyEntityType);

            // Entity type with a single primitive collection property
            var singleComplexCollectionPropertyEntityType = new EdmEntityType(DefaultNamespaceName, "SingleComplexCollectionPropertyEntityType");
            singleComplexCollectionPropertyEntityType.AddKeys(singleComplexCollectionPropertyEntityType.AddStructuralProperty("ID", Int32TypeRef));
            singleComplexCollectionPropertyEntityType.AddStructuralProperty("ComplexCollectionProp", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, isNullable: true)));
            entityContainer.AddEntitySet("SingleComplexCollectionPropertyEntityType", singleComplexCollectionPropertyEntityType);
            model.AddElement(singleComplexCollectionPropertyEntityType);

            // Entity type with different property kinds
            var differentPropertyKindsEntityType = new EdmEntityType(DefaultNamespaceName, "DifferentPropertyKindsEntityType");
            differentPropertyKindsEntityType.AddKeys(differentPropertyKindsEntityType.AddStructuralProperty("ID", Int32TypeRef));
            differentPropertyKindsEntityType.AddStructuralProperty("ComplexProp", new EdmComplexTypeReference(complexType, isNullable: true));
            differentPropertyKindsEntityType.AddStructuralProperty("PrimitiveCollectionProp", EdmCoreModel.GetCollection(Int32TypeRef));
            differentPropertyKindsEntityType.AddStructuralProperty("Int32Prop", EdmPrimitiveTypeKind.Int32, isNullable: false);
            differentPropertyKindsEntityType.AddStructuralProperty("ComplexCollectionProp", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, isNullable: true)));
            entityContainer.AddEntitySet("DifferentPropertyKindsEntityType", differentPropertyKindsEntityType);
            model.AddElement(differentPropertyKindsEntityType);
            #endregion Entity types

            #region Complex types
            // Empty complex type
            model.AddElement(new EdmComplexType(DefaultNamespaceName, "EmptyComplexType"));

            // Complex type with a single primitive property
            var singlePrimitivePropertyComplexType = new EdmComplexType(DefaultNamespaceName, "SinglePrimitivePropertyComplexType");
            singlePrimitivePropertyComplexType.AddStructuralProperty("Int32Prop", Int32NullableTypeRef);
            model.AddElement(singlePrimitivePropertyComplexType);

            // Complex type with all primitive properties
            var allPrimitivePropertiesComplexType = new EdmComplexType(DefaultNamespaceName, "AllPrimitivePropertiesComplexType");
            allPrimitivePropertiesComplexType.AddStructuralProperty("BoolProp", EdmPrimitiveTypeKind.Boolean, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("Int16Prop", EdmPrimitiveTypeKind.Int16, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("Int32Prop", EdmPrimitiveTypeKind.Int32, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("Int64Prop", EdmPrimitiveTypeKind.Int64, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("ByteProp", EdmPrimitiveTypeKind.Byte, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("SByteProp", EdmPrimitiveTypeKind.SByte, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("SingleProp", EdmPrimitiveTypeKind.Single, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("DoubleProp", EdmPrimitiveTypeKind.Double, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("DecimalProp", EdmPrimitiveTypeKind.Decimal, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("DateTimeOffsetProp", EdmPrimitiveTypeKind.DateTimeOffset, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("TimeProp", EdmPrimitiveTypeKind.Duration, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("GuidProp", EdmPrimitiveTypeKind.Guid, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("StringProp", EdmPrimitiveTypeKind.String, isNullable: false);
            allPrimitivePropertiesComplexType.AddStructuralProperty("BinaryProp", EdmPrimitiveTypeKind.Binary, isNullable: false);
            model.AddElement(allPrimitivePropertiesComplexType);

            // Complex type with a single complex property
            var singleComplexPropertyComplexType = new EdmComplexType(DefaultNamespaceName, "SingleComplexPropertyComplexType");
            singleComplexPropertyComplexType.AddStructuralProperty("ComplexProp", new EdmComplexTypeReference(complexType, isNullable: true));
            model.AddElement(singleComplexPropertyComplexType);

            // Complex type with a single primitive collection property
            var singlePrimitiveCollectionPropertyComplexType = new EdmComplexType(DefaultNamespaceName, "SinglePrimitiveCollectionPropertyComplexType");
            singlePrimitiveCollectionPropertyComplexType.AddStructuralProperty("PrimitiveCollectionProp", EdmCoreModel.GetCollection(Int32TypeRef));
            model.AddElement(singlePrimitiveCollectionPropertyComplexType);

            // Complex type with a single primitive collection property
            var singleComplexCollectionPropertyComplexType = new EdmComplexType(DefaultNamespaceName, "SingleComplexCollectionPropertyComplexType");
            singleComplexCollectionPropertyComplexType.AddStructuralProperty("ComplexCollectionProp", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, isNullable: true)));
            model.AddElement(singleComplexCollectionPropertyComplexType);

            // Complex type with different property kinds
            var differentPropertyKindsComplexType = new EdmComplexType(DefaultNamespaceName, "DifferentPropertyKindsComplexType");
            differentPropertyKindsComplexType.AddStructuralProperty("ComplexProp", new EdmComplexTypeReference(complexType, isNullable: true));
            differentPropertyKindsComplexType.AddStructuralProperty("PrimitiveCollectionProp", EdmCoreModel.GetCollection(Int32TypeRef));
            differentPropertyKindsComplexType.AddStructuralProperty("Int32Prop", EdmPrimitiveTypeKind.Int32);
            differentPropertyKindsComplexType.AddStructuralProperty("ComplexCollectionProp", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, isNullable: true)));
            model.AddElement(differentPropertyKindsComplexType);
            #endregion Complex types

            return(model);
        }
コード例 #28
0
        /// <summary>
        /// Build a test model shared across several tests.
        /// </summary>
        /// <returns>Returns the test model.</returns>
        public static EdmModel BuildTestModel()
        {
            // The metadata model
            var model = new EdmModel();

            var addressType = new EdmComplexType(DefaultNamespaceName, "Address");

            addressType.AddStructuralProperty("Street", StringNullableTypeRef);
            addressType.AddStructuralProperty("Zip", Int32TypeRef);
            addressType.AddStructuralProperty("SubAddress", new EdmComplexTypeReference(addressType, isNullable: false));
            model.AddElement(addressType);

            var officeType = new EdmEntityType(DefaultNamespaceName, "OfficeType");

            officeType.AddKeys(officeType.AddStructuralProperty("Id", Int32TypeRef));
            officeType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, isNullable: false));
            model.AddElement(officeType);

            var officeWithNumberType = new EdmEntityType(DefaultNamespaceName, "OfficeWithNumberType", officeType);

            officeWithNumberType.AddStructuralProperty("Number", Int32TypeRef);
            model.AddElement(officeWithNumberType);

            var cityType = new EdmEntityType(DefaultNamespaceName, "CityType");

            cityType.AddKeys(cityType.AddStructuralProperty("Id", Int32TypeRef));
            cityType.AddStructuralProperty("Name", StringNullableTypeRef);
            cityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "CityHall", Target = officeType, TargetMultiplicity = EdmMultiplicity.Many
            });
            cityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "DOL", Target = officeType, TargetMultiplicity = EdmMultiplicity.Many
            });
            cityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "PoliceStation", Target = officeType, TargetMultiplicity = EdmMultiplicity.One
            });
            cityType.AddStructuralProperty("Skyline", EdmPrimitiveTypeKind.Stream, isNullable: false);
            cityType.AddStructuralProperty("MetroLanes", EdmCoreModel.GetCollection(StringNullableTypeRef));
            model.AddElement(cityType);

            var metropolitanCityType = new EdmEntityType(DefaultNamespaceName, "MetropolitanCityType", cityType);

            metropolitanCityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ContainedOffice", Target = officeType, TargetMultiplicity = EdmMultiplicity.Many, ContainsTarget = true
            });
            officeType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ContainedCity", Target = metropolitanCityType, TargetMultiplicity = EdmMultiplicity.One, ContainsTarget = true
            });
            model.AddElement(metropolitanCityType);

            var cityWithMapType = new EdmEntityType(DefaultNamespaceName, "CityWithMapType", cityType, false, false, true);

            model.AddElement(cityWithMapType);

            var cityOpenType = new EdmEntityType(DefaultNamespaceName, "CityOpenType", cityType, isAbstract: false, isOpen: true);

            model.AddElement(cityOpenType);

            var personType = new EdmEntityType(DefaultNamespaceName, "Person");

            personType.AddKeys(personType.AddStructuralProperty("Id", Int32TypeRef));
            personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "Friend", Target = personType, TargetMultiplicity = EdmMultiplicity.Many
            });
            model.AddElement(personType);

            var employeeType = new EdmEntityType(DefaultNamespaceName, "Employee", personType);

            employeeType.AddStructuralProperty("CompanyName", StringNullableTypeRef);
            model.AddElement(employeeType);

            var managerType = new EdmEntityType(DefaultNamespaceName, "Manager", employeeType);

            managerType.AddStructuralProperty("Level", Int32TypeRef);
            model.AddElement(managerType);

            var container = new EdmEntityContainer(DefaultNamespaceName, "DefaultContainer");

            model.AddElement(container);

            container.AddEntitySet("Offices", officeType);
            container.AddEntitySet("Cities", cityType);
            container.AddEntitySet("MetropolitanCities", metropolitanCityType);
            container.AddEntitySet("Persons", personType);
            container.AddEntitySet("Employee", employeeType);
            container.AddEntitySet("Manager", managerType);
            container.AddSingleton("Boss", personType);

            // Fixup will set DefaultContainer\TopLevelEntitySet\AssociationSet
            model.Fixup();

            // NOTE: Function import parameters and return types must be nullable as per current CSDL spec
            var serviceOp = container.AddFunctionAndFunctionImport(model, "ServiceOperation1", Int32NullableTypeRef, null, false /*isComposable*/, false /*isBound*/);

            serviceOp.Function.AsEdmFunction().AddParameter("a", Int32NullableTypeRef);
            serviceOp.Function.AsEdmFunction().AddParameter("b", StringNullableTypeRef);

            container.AddFunctionAndFunctionImport(model, "PrimitiveResultOperation", Int32NullableTypeRef, null, false /*isComposable*/, false /*isBound*/);
            container.AddFunctionAndFunctionImport(model, "ComplexResultOperation", new EdmComplexTypeReference(addressType, isNullable: true), null, false /*isComposable*/, false /*isBound*/);
            container.AddFunctionAndFunctionImport(model, "PrimitiveCollectionResultOperation", EdmCoreModel.GetCollection(Int32NullableTypeRef), null, false /*isComposable*/, false /*isBound*/);
            container.AddFunctionAndFunctionImport(model, "ComplexCollectionResultOperation", EdmCoreModel.GetCollection(new EdmComplexTypeReference(addressType, isNullable: true)), null, false /*isComposable*/, false /*isBound*/);

            // Overload with 0 Param
            container.AddFunctionAndFunctionImport(model, "FunctionImportWithOverload", Int32NullableTypeRef, null, false /*isComposable*/, false /*isBound*/);

            // Overload with 1 Param
            var overloadWithOneParam = container.AddFunctionAndFunctionImport(model, "FunctionImportWithOverload", Int32NullableTypeRef, null, false /*isComposable*/, false /*isBound*/);

            overloadWithOneParam.Function.AsEdmFunction().AddParameter("p1", new EdmEntityTypeReference(cityWithMapType, isNullable: true));

            // Overload with 2 Params
            var overloadWithTwoParams = container.AddFunctionAndFunctionImport(model, "FunctionImportWithOverload", Int32NullableTypeRef, null, false /*isComposable*/, false /*isBound*/);

            overloadWithTwoParams.Function.AsEdmFunction().AddParameter("p1", new EdmEntityTypeReference(cityType, isNullable: true));
            overloadWithTwoParams.Function.AsEdmFunction().AddParameter("p2", StringNullableTypeRef);

            // Overload with 5 Params
            var overloadWithFiveParams = container.AddFunctionAndFunctionImport(model, "FunctionImportWithOverload", Int32NullableTypeRef, null, false /*isComposable*/, false /*isBound*/);

            overloadWithFiveParams.Function.AsEdmFunction().AddParameter("p1", EdmCoreModel.GetCollection(new EdmEntityTypeReference(cityType, isNullable: true)));
            overloadWithFiveParams.Function.AsEdmFunction().AddParameter("p2", EdmCoreModel.GetCollection(StringNullableTypeRef));
            overloadWithFiveParams.Function.AsEdmFunction().AddParameter("p3", StringNullableTypeRef);
            overloadWithFiveParams.Function.AsEdmFunction().AddParameter("p4", new EdmComplexTypeReference(addressType, isNullable: true));
            overloadWithFiveParams.Function.AsEdmFunction().AddParameter("p5", EdmCoreModel.GetCollection(new EdmComplexTypeReference(addressType, isNullable: true)));

            return(model);
        }
コード例 #29
0
        /// <summary>
        /// Creates a set of models.
        /// </summary>
        /// <returns>List of interesting models.</returns>
        public static IEnumerable <IEdmModel> CreateModels()
        {
            //
            // NOTE: we only create a few models here since we mostly rely on EdmLib to test
            //       model serialization/deserialization for us
            //
            // Empty model
            EdmModel emptyModel = new EdmModel();

            emptyModel.AddElement(new EdmEntityContainer("DefaultNamespace", "DefaultContainer"));
            yield return(emptyModel.Fixup());

            // Model with a single entity type
            EdmModel modelWithSingleEntityType = new EdmModel();
            var      singletonEntityType       = new EdmEntityType(DefaultNamespaceName, "SingletonEntityType");

            singletonEntityType.AddKeys(singletonEntityType.AddStructuralProperty("Id", Int32TypeRef));
            singletonEntityType.AddStructuralProperty("Name", StringNullableTypeRef);
            modelWithSingleEntityType.AddElement(singletonEntityType);
            modelWithSingleEntityType.Fixup();
            yield return(modelWithSingleEntityType);

            // Model with a single complex type
            EdmModel modelWithSingleComplexType = new EdmModel();
            var      singletonComplexType       = new EdmComplexType(DefaultNamespaceName, "SingletonComplexType");

            singletonComplexType.AddStructuralProperty("City", StringNullableTypeRef);
            modelWithSingleComplexType.AddElement(singletonComplexType);
            modelWithSingleComplexType.Fixup();
            yield return(modelWithSingleComplexType);

            // Model with a collection property
            EdmModel modelWithCollectionProperty = new EdmModel();
            var      complexTypeWithCollection   = new EdmComplexType(DefaultNamespaceName, "ComplexTypeWithCollection");

            complexTypeWithCollection.AddStructuralProperty("Cities", EdmCoreModel.GetCollection(StringNullableTypeRef));
            modelWithCollectionProperty.AddElement(complexTypeWithCollection);
            modelWithCollectionProperty.Fixup();
            yield return(modelWithCollectionProperty);

            // Model with an open type
            EdmModel modelWithOpenType = new EdmModel();
            var      openType          = new EdmEntityType(DefaultNamespaceName, "OpenEntityType", baseType: null, isAbstract: false, isOpen: true);

            openType.AddKeys(openType.AddStructuralProperty("Id", Int32TypeRef));
            modelWithOpenType.AddElement(openType);

            var containerForModelWithOpenType = new EdmEntityContainer(DefaultNamespaceName, "DefaultContainer");

            containerForModelWithOpenType.AddEntitySet("OpenEntityType", openType);
            modelWithOpenType.AddElement(containerForModelWithOpenType);
            yield return(modelWithOpenType);

            // Model with a named stream
            EdmModel modelWithNamedStream  = new EdmModel();
            var      namedStreamEntityType = new EdmEntityType(DefaultNamespaceName, "NamedStreamEntityType");

            namedStreamEntityType.AddKeys(namedStreamEntityType.AddStructuralProperty("Id", Int32TypeRef));
            namedStreamEntityType.AddStructuralProperty("NamedStream", EdmPrimitiveTypeKind.Stream, isNullable: false);
            modelWithNamedStream.AddElement(namedStreamEntityType);

            var containerForModelWithNamedStream = new EdmEntityContainer(DefaultNamespaceName, "DefaultContainer");

            containerForModelWithNamedStream.AddEntitySet("NamedStreamEntityType", namedStreamEntityType);
            modelWithNamedStream.AddElement(containerForModelWithNamedStream);
            yield return(modelWithNamedStream);

            // OData Shared Test Model
            yield return(BuildTestModel());

            // Model with OData-specific attribute annotations
            yield return(BuildODataAnnotationTestModel(true));

#if !NETCOREAPP1_0
            // Astoria Default Test Model
            yield return(BuildDefaultAstoriaTestModel());
#endif
        }
コード例 #30
0
        private static IEdmModel GetUnTypedEdmModel()
        {
            EdmModel model = new EdmModel();

            // Enum type "Color"
            EdmEnumType colorEnum = new EdmEnumType("NS", "Color");

            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Red", new EdmEnumMemberValue(0)));
            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Blue", new EdmEnumMemberValue(1)));
            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Green", new EdmEnumMemberValue(2)));
            model.AddElement(colorEnum);

            // complex type "Address"
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            // derived complex type "SubAddress"
            EdmComplexType subAddress = new EdmComplexType("NS", "SubAddress", address);

            subAddress.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Double);
            model.AddElement(subAddress);

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

            customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            customer.AddStructuralProperty("Location", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(customer);

            // derived entity type special customer
            EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer);

            specialCustomer.AddStructuralProperty("Title", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialCustomer);

            // entity sets
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");

            model.AddElement(container);
            container.AddEntitySet("FCustomers", customer);

            EdmComplexTypeReference    complexType           = new EdmComplexTypeReference(address, isNullable: false);
            EdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(complexType));

            EdmEnumTypeReference       enumType           = new EdmEnumTypeReference(colorEnum, isNullable: false);
            EdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(enumType));

            EdmEntityTypeReference     entityType           = new EdmEntityTypeReference(customer, isNullable: false);
            EdmCollectionTypeReference entityCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(entityType));

            IEdmTypeReference          intType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: true);
            EdmCollectionTypeReference primitiveCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(intType));

            // bound functions
            BoundFunction(model, "IntCollectionFunction", "intValues", primitiveCollectionType, entityType);

            BoundFunction(model, "ComplexFunction", "address", complexType, entityType);

            BoundFunction(model, "ComplexCollectionFunction", "addresses", complexCollectionType, entityType);

            BoundFunction(model, "EnumFunction", "color", enumType, entityType);

            BoundFunction(model, "EnumCollectionFunction", "colors", enumCollectionType, entityType);

            BoundFunction(model, "EntityFunction", "customer", entityType, entityType);

            BoundFunction(model, "CollectionEntityFunction", "customers", entityCollectionType, entityType);

            // unbound functions
            UnboundFunction(container, "UnboundIntCollectionFunction", "intValues", primitiveCollectionType);

            UnboundFunction(container, "UnboundComplexFunction", "address", complexType);

            UnboundFunction(container, "UnboundComplexCollectionFunction", "addresses", complexCollectionType);

            UnboundFunction(container, "UnboundEnumFunction", "color", enumType);

            UnboundFunction(container, "UnboundEnumCollectionFunction", "colors", enumCollectionType);

            UnboundFunction(container, "UnboundEntityFunction", "customer", entityType);

            UnboundFunction(container, "UnboundCollectionEntityFunction", "customers", entityCollectionType);

            // bound to collection
            BoundToCollectionFunction(model, "BoundToCollectionFunction", "p", intType, entityType);

            model.SetAnnotationValue <BindableOperationFinder>(model, new BindableOperationFinder(model));
            return(model);
        }