public static void Register(HttpConfiguration config)
        {
            var model2 = EdmxReader.Parse(XmlReader.Create("http://localhost:9091/odata/$metadata"));

            var model = new EdmModel();
            model.AddReferencedModel(model2);

            var reference = new EdmReference("http://localhost:9091/odata/$metadata");
            reference.AddInclude(new EdmInclude("Model2", "SampleService2.Models"));
            model.SetEdmReferences(new List<IEdmReference> { reference });

            var container = new EdmEntityContainer("NS1", "Default");
            var order = model2.FindDeclaredType("SampleService2.Models.Order") as IEdmEntityType;
            model2.SetAnnotationValue<ClrTypeAnnotation>(order, new ClrTypeAnnotation(typeof(Order)));
            container.AddEntitySet("Orders", order);
            model.AddElement(container);

            var product = new EdmEntityType("NS1", "Product");
            product.AddKeys(product.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            product.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(product);

            config.MapODataServiceRoute("odata", "odata", model);
        }
        public void EdmxRoundTripTests_EdmxReferences()
        {
            string mainEdmx;
            string referencedEdmx1;
            string referencedEdmx2;
            IEdmModel mainModel;
            EdmxModelBuilder.GetReferencedModelEdmx(out mainModel, out mainEdmx, out referencedEdmx1, out referencedEdmx2);
            IEnumerable<EdmError> errors;
            bool valid = mainModel.Validate(out errors);
            Assert.IsTrue(valid);

            // verify reading edmx:Reference
            List<IEdmReference> references = mainModel.GetEdmReferences().ToList();
            Assert.AreEqual(2, references.Count);
            Assert.AreEqual("VPCT", references[0].Includes.First().Alias);
            Assert.AreEqual("NS.Ref1", references[0].Includes.First().Namespace);

            // verify writing edmx:Reference
            // add a new <edmx:reference>
            EdmReference newReference = new EdmReference("http://addedUrl/addedEdm.xml");
            newReference.AddInclude(new EdmInclude("adhoc_Alias", "adhoc_Namespace"));
            List<IEdmReference> newReferences = new List<IEdmReference>();
            newReferences.AddRange(references);
            newReferences.Add(newReference);
            mainModel.SetEdmReferences(newReferences);
            string actualEdmx = GetEdmx(mainModel, EdmxTarget.OData);

            // add new Include to verify: Namespace=""adhoc_Namespace"" Alias=""adhoc_Alias""
            mainEdmx = mainEdmx.Replace("  <edmx:DataServices>",
             @"  <edmx:Reference Uri=""http://addedUrl/addedEdm.xml"">
    <edmx:Include Namespace=""adhoc_Namespace"" Alias=""adhoc_Alias"" />
  </edmx:Reference>
  <edmx:DataServices>");

            // after deserialization & serialization, the alias'ed 'CT.Customer' becomes qualified name 'NS1.Customer',
            // so make some adjustments for verification: 
            actualEdmx = actualEdmx.Replace("EntityType=\"NS1.Customer\"", "EntityType=\"CT.Customer\"");
            actualEdmx = actualEdmx.Replace("EntityType=\"NS.Ref1.VipCustomer\"", "EntityType=\"VPCT.VipCustomer\"");
            actualEdmx = actualEdmx.Replace("EntityType=\"NS.Ref2.VipCard\"", "EntityType=\"VPCD.VipCard\"");
            valid = XElement.DeepEquals(XElement.Parse(mainEdmx), XElement.Parse(actualEdmx));
            Assert.IsTrue(valid, "Invalid actual edmx.");
        }
Beispiel #3
0
        /// <summary>
        /// TODO: use XmlDocumentParser
        /// </summary>
        private void ParseReferenceElement()
        {
            // read 'Uri' attribute
            EdmReference result = new EdmReference(this.GetAttributeValue(null, CsdlConstants.Attribute_Uri));
            if (this.reader.IsEmptyElement)
            {
                this.reader.Read();
                this.edmReferences.Add(result);
                return;
            }

            this.reader.Read();
            while (this.reader.NodeType != XmlNodeType.EndElement)
            {
                while (this.reader.NodeType == XmlNodeType.Whitespace && this.reader.Read())
                { // read white spaces. can be an extension method.
                }

                if (this.reader.NodeType != XmlNodeType.Element)
                {
                    break;
                }

                if (this.reader.LocalName == CsdlConstants.Element_Include)
                {
                    // parse: <edmx:Include Alias="IoTDeviceModel" Namespace="Microsoft.IntelligentSystems.DeviceModel.Vocabulary.V1"/>
                    IEdmInclude tmp = new EdmInclude(this.GetAttributeValue(null, CsdlConstants.Attribute_Alias), this.GetAttributeValue(null, CsdlConstants.Attribute_Namespace));
                    result.AddInclude(tmp);
                }
                else if (this.reader.LocalName == CsdlConstants.Element_IncludeAnnotations)
                {
                    // parse: <edmx:IncludeAnnotations TermNamespace="org.example.hcm" Qualifier="Tablet" TargetNamespace="com.contoso.Person" />
                    IEdmIncludeAnnotations tmp = new EdmIncludeAnnotations(this.GetAttributeValue(null, CsdlConstants.Attribute_TermNamespace), this.GetAttributeValue(null, CsdlConstants.Attribute_Qualifier), this.GetAttributeValue(null, CsdlConstants.Attribute_TargetNamespace));
                    result.AddIncludeAnnotations(tmp);
                }
                else if (this.reader.LocalName == CsdlConstants.Element_Annotation)
                {
                    this.reader.Skip();
                    this.RaiseError(EdmErrorCode.UnexpectedXmlElement, Edm.Strings.XmlParser_UnexpectedElement(this.reader.LocalName));
                    continue;
                }
                else
                {
                    this.RaiseError(EdmErrorCode.UnexpectedXmlElement, Edm.Strings.XmlParser_UnexpectedElement(this.reader.LocalName));
                }

                if (!this.reader.IsEmptyElement)
                {
                    this.reader.Read();
                    while (this.reader.NodeType == XmlNodeType.Whitespace && this.reader.Read())
                    { // read white spaces. can be an extension method.
                    }

                    Debug.Assert(this.reader.NodeType == XmlNodeType.EndElement, "The XmlReader should be at the end of element");
                }

                this.reader.Read();
            }

            Debug.Assert(this.reader.NodeType == XmlNodeType.EndElement, "The XmlReader should be at the end of element");
            this.reader.Read();
            this.edmReferences.Add(result);
        }
Beispiel #4
0
        private static void MutualReferenceByCodeDemo()
        {
            Console.WriteLine("MutualReferenceByCodeDemo");

            var subModel1 = new EdmModel();
            var complex1 = new EdmComplexType("NS1", "Complex1");
            subModel1.AddElement(complex1);
            var reference1 = new EdmReference("http://model2");
            reference1.AddInclude(new EdmInclude("Alias2", "NS2"));
            var references1 = new List<IEdmReference> { reference1 };
            subModel1.SetEdmReferences(references1);

            var subModel2 = new EdmModel();
            var complex2 = new EdmComplexType("NS2", "Complex2");
            subModel2.AddElement(complex2);
            var reference2 = new EdmReference("http://model1");
            reference2.AddInclude(new EdmInclude("Alias1", "NS1"));
            var references2 = new List<IEdmReference> { reference2 };
            subModel2.SetEdmReferences(references2);

            complex1.AddStructuralProperty("Prop", new EdmComplexTypeReference(complex2, true));
            complex2.AddStructuralProperty("Prop", new EdmComplexTypeReference(complex1, true));

            var mainModel = new EdmModel();
            var complex3 = new EdmComplexType("NS", "Complex3");
            mainModel.AddElement(complex3);
            complex3.AddStructuralProperty("Prop1", new EdmComplexTypeReference(complex1, true));
            complex3.AddStructuralProperty("Prop2", new EdmComplexTypeReference(complex2, true));
            var references3 = new List<IEdmReference> { reference1, reference2 };
            mainModel.SetEdmReferences(references3);

            ShowModel(mainModel);
            ShowModel(subModel1);
            ShowModel(subModel2);
        }