示例#1
0
        /// <summary>
        /// Build a test model shared across several tests.
        /// </summary>
        /// <param name="addAnnotations">true if the annotations should be added upon construction; otherwise false.</param>
        /// <returns>Returns the test model.</returns>
        public static IEdmModel BuildODataAnnotationTestModel(bool addAnnotations)
        {
            // The metadata model with OData-specific annotations
            // - default entity container annotation
            // - HasStream annotation on entity type
            // - MimeType annotation on primitive property
            // - MimeType annotation on service operation
            EdmModel model = new EdmModel();

            var addressType = new EdmComplexType(DefaultNamespaceName, "Address");
            addressType.AddStructuralProperty("Street", StringNullableTypeRef);
            var zipProperty = addressType.AddStructuralProperty("Zip", Int32TypeRef);
            model.AddElement(addressType);
            if (addAnnotations)
            {
                model.SetMimeType(zipProperty, "text/plain");
            }

            var personType = new EdmEntityType(DefaultNamespaceName, "PersonType", null, false, false, true);
            personType.AddKeys(personType.AddStructuralProperty("Id", Int32TypeRef));
            var nameProperty = personType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(isNullable: false));
            personType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, isNullable: false));
            personType.AddStructuralProperty("Picture", EdmPrimitiveTypeKind.Stream, isNullable: false);
            model.AddElement(personType);
            if (addAnnotations)
            {
                model.SetMimeType(nameProperty, "text/plain");
            }

            // set the default container
            EdmEntityContainer container = new EdmEntityContainer(DefaultNamespaceName, "DefaultContainer");
            model.AddElement(container);

            container.AddEntitySet("People", personType);

            // NOTE: Function import parameters and return types must be nullable as per current CSDL spec
            var serviceOp = container.AddFunctionAndFunctionImport(model, "ServiceOperation1", Int32NullableTypeRef);
            var serviceOpFunc = serviceOp.Function.AsEdmFunction();
            serviceOpFunc.AddParameter("a", Int32NullableTypeRef);
            serviceOpFunc.AddParameter("b", StringNullableTypeRef);

            if (addAnnotations)
            {
                model.SetMimeType(serviceOpFunc, "img/jpeg");
            }

            return model;
        }