private static IEdmModel GetModel() { var model2 = CsdlReader.Parse(XmlReader.Create("https://localhost:44350/odata/$metadata")); var model = new EdmModel(); model.AddReferencedModel(model2); var reference = new EdmReference(new Uri("https://localhost:44350/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); return(model); }
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 Uri in EdmReference string uriString = "http://addedUrl/addedEdm.xml"; EdmReference newReference = new EdmReference(new Uri(uriString)); Assert.AreEqual(uriString, EdmValueWriter.UriAsXml(newReference.Uri)); // verify writing edmx:Reference // add a new <edmx:reference> 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, CsdlTarget.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."); }
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); }
private IEnumerable <EdmReference> CreateReferences() { EdmReference MakeReference(string alias, string @namespace, IEdmModel model) { var reference = new EdmReference(new Uri("http://unknown.com")); reference.AddInclude(new EdmInclude(alias, @namespace)); return(reference); } // https://devblogs.microsoft.com/odata/tutorial-sample-refering-when-constructing-edm-model/ return (from referenced in env.References select MakeReference(referenced.alias, referenced.@namespace, referenced.model)); }
/// <summary> /// Parse the <see cref="JsonElement"/> to a <see cref="IEdmReference"/>. /// </summary> /// <param name="url">The reference Url string.</param> /// <param name="element">The input JSON element.</param> /// <param name="context">The parser context.</param> /// <returns>null or parsed <see cref="IEdmReference"/>.</returns> internal static IEdmReference ParseReference(string url, JsonElement element, JsonParserContext context) { // The value of each reference object is an object. if (!element.ValidateValueKind(JsonValueKind.Object, context)) { return(null); } IList <IEdmInclude> includes = null; IList <IEdmIncludeAnnotations> includeAnnotations = null; element.ParseAsObject(context, (propertyName, propertyValue) => { // The reference object MAY contain the members $Include and $IncludeAnnotations as well as annotations. switch (propertyName) { case "$Include": // The value of $Include is an array. // Array items are objects that MUST contain the member $Namespace and MAY contain the member $Alias. includes = propertyValue.ParseAsArray(context, ParseInclude); break; case "$IncludeAnnotations": // The value of $IncludeAnnotations is an array. // Array items are objects that MUST contain the member $TermNamespace and MAY contain the members $Qualifier and $TargetNamespace. includeAnnotations = propertyValue.ParseAsArray(context, ParseIncludeAnnotations); break; default: // The reference objects MAY contain annotations.However, EdmReference doesn't support annotation. // So, skip the annotation. context.ReportError(EdmErrorCode.UnexpectedElement, Strings.CsdlJsonParser_UnexpectedJsonMember(context.Path, propertyValue.ValueKind)); break; } }); EdmReference edmReference = new EdmReference(new Uri(url, UriKind.RelativeOrAbsolute)); includes.ForEach(i => edmReference.AddInclude(i)); includeAnnotations.ForEach(i => edmReference.AddIncludeAnnotations(i)); return(edmReference); }
/// <summary> /// TODO: use XmlDocumentParser /// </summary> private void ParseReferenceElement() { // read 'Uri' attribute EdmReference result = new EdmReference(new Uri(this.GetAttributeValue(null, CsdlConstants.Attribute_Uri), UriKind.RelativeOrAbsolute)); 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); }