private static IEdmModel GetEdmModel() { EdmModel _builder = new EdmModel(); // Create Car Entity EdmEntityType car = new EdmEntityType("Part11.Models", "Car"); car.AddKeys(car.AddStructuralProperty("Make", EdmPrimitiveTypeKind.String), car.AddStructuralProperty("Model", EdmPrimitiveTypeKind.String)); car.AddStructuralProperty("Colour", EdmPrimitiveTypeKind.String); car.AddStructuralProperty("Price", EdmPrimitiveTypeKind.Double); var cat = car.AddStructuralProperty("Category", EdmPrimitiveTypeKind.String, false); _builder.AddAlternateKeyAnnotation(car, new Dictionary <string, IEdmProperty> { { "Category", cat } }); _builder.AddElement(car); // Create Container EdmEntityContainer container = new EdmEntityContainer("default", "Container"); // Add the Cars Entity into the Container EdmEntitySet cars = container.AddEntitySet("Cars", car); _builder.AddElement(container); return(_builder); }
private static void SetOrderAlternateKey(EdmModel model) { // Add multiple alternate keys using the Term/annotation methods. // It's using 'Org.OData.Core.V1.AlternateKeys' var order = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Order"); var name = order.FindProperty("Name"); model.AddAlternateKeyAnnotation(order, new Dictionary <string, IEdmProperty> { { "Name", name }, }); var token = order.FindProperty("Token"); model.AddAlternateKeyAnnotation(order, new Dictionary <string, IEdmProperty> { { "Token", token }, }); // ODL doesn't support Org.OData.Core.V1.AlternateKeys to do the uri parsing /* * var alternateKeysCollection = new List<IEdmExpression>(); * foreach (string item in new [] { "Name", "Token"}) * { * List<IEdmExpression> propertyRefs = new List<IEdmExpression>(); * * IEdmRecordExpression propertyRef = new EdmRecordExpression( * new EdmPropertyConstructor("Alias", new EdmStringConstant(item)), * new EdmPropertyConstructor("Name", new EdmPropertyPathExpression(item))); * propertyRefs.Add(propertyRef); * * EdmRecordExpression alternateKeyRecord = new EdmRecordExpression( * new EdmPropertyConstructor("Key", new EdmCollectionExpression(propertyRefs))); * * alternateKeysCollection.Add(alternateKeyRecord); * } * * var term = model.FindTerm("Org.OData.Core.V1.AlternateKeys"); * var annotation = new EdmVocabularyAnnotation(order, term, new EdmCollectionExpression(alternateKeysCollection)); * * annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); * model.SetVocabularyAnnotation(annotation); */ }
private static void SetCustomerAlternateKey(EdmModel model) { // Add one alternate key using the extension method. // It's using 'OData.Community.Keys.V1.AlternateKeys' var customer = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); var ssn = customer.FindProperty("SSN"); model.AddAlternateKeyAnnotation(customer, new Dictionary <string, IEdmProperty> { { "SSN", ssn } }); }
private static void SetPersonAlternateKey(EdmModel model) { // Add composed alternate keys using the Term/annotation methods. // It's using 'Org.OData.Core.V1.AlternateKeys' var person = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Person"); var cr = person.FindProperty("CountryOrRegion"); var passport = person.FindProperty("Passport"); model.AddAlternateKeyAnnotation(person, new Dictionary <string, IEdmProperty> { { "c_or_r", cr }, { "passport", passport }, }); // ODL doesn't support Org.OData.Core.V1.AlternateKeys to do the uri parsing /* * List<IEdmExpression> propertyRefs = new List<IEdmExpression>(); * * IEdmRecordExpression propertyRef = new EdmRecordExpression( * new EdmPropertyConstructor("Alias", new EdmStringConstant("CountryOrRegion")), * new EdmPropertyConstructor("Name", new EdmPropertyPathExpression("CountryOrRegion"))); * propertyRefs.Add(propertyRef); * * propertyRef = new EdmRecordExpression( * new EdmPropertyConstructor("Alias", new EdmStringConstant("Passport")), * new EdmPropertyConstructor("Name", new EdmPropertyPathExpression("Passport"))); * propertyRefs.Add(propertyRef); * * EdmRecordExpression alternateKeyRecord = new EdmRecordExpression( * new EdmPropertyConstructor("Key", new EdmCollectionExpression(propertyRefs))); * * var alternateKeysCollection = new List<IEdmExpression>(); * alternateKeysCollection.Add(alternateKeyRecord); * * var term = model.FindTerm("Org.OData.Core.V1.AlternateKeys"); * var annotation = new EdmVocabularyAnnotation(person, term, new EdmCollectionExpression(alternateKeysCollection)); * * annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); * model.SetVocabularyAnnotation(annotation); */ }
public void ODataPathSegmentToTemplateHandler_Handles_AlternateKey() { // Arrange EdmModel model = new EdmModel(); EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); IEdmStructuralProperty code = customer.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Int32); model.AddElement(customer); EdmEntityContainer entityContainer = new EdmEntityContainer("NS", "Default"); EdmEntitySet customers = entityContainer.AddEntitySet("Customers", customer); model.AddElement(entityContainer); IDictionary <string, IEdmProperty> alternateKeys = new Dictionary <string, IEdmProperty> { { "Code", code } }; model.AddAlternateKeyAnnotation(customer, alternateKeys); IDictionary <string, object> keys = new Dictionary <string, object> { { "Code", "{Code}" } }; KeySegment segment = new KeySegment(keys, customer, customers); ODataPathSegmentToTemplateHandler handler = new ODataPathSegmentToTemplateHandler(model); // Act handler.Handle(segment); // Assert ODataSegmentTemplate segmentTemplate = Assert.Single(handler.Templates); Assert.IsType <KeySegmentTemplate>(segmentTemplate); }
public static IEdmModel GetEdmModel() { if (_edmModel != null) { return(_edmModel); } EdmModel model = new EdmModel(); // entity type 'Customer' with single alternate keys EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); var ssn = customer.AddStructuralProperty("SSN", EdmPrimitiveTypeKind.String); model.AddAlternateKeyAnnotation(customer, new Dictionary <string, IEdmProperty> { { "SSN", ssn } }); model.AddElement(customer); // entity type 'Order' with multiple alternate keys EdmEntityType order = new EdmEntityType("NS", "Order"); order.AddKeys(order.AddStructuralProperty("OrderId", EdmPrimitiveTypeKind.Int32)); var orderName = order.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); var orderToken = order.AddStructuralProperty("Token", EdmPrimitiveTypeKind.Guid); order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32); model.AddAlternateKeyAnnotation(order, new Dictionary <string, IEdmProperty> { { "Name", orderName } }); model.AddAlternateKeyAnnotation(order, new Dictionary <string, IEdmProperty> { { "Token", orderToken } }); model.AddElement(order); // entity type 'Person' with composed alternate keys EdmEntityType person = new EdmEntityType("NS", "Person"); person.AddKeys(person.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); var country = person.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String); var passport = person.AddStructuralProperty("Passport", EdmPrimitiveTypeKind.String); model.AddAlternateKeyAnnotation(person, new Dictionary <string, IEdmProperty> { { "Country", country }, { "Passport", passport } }); model.AddElement(person); // entity sets EdmEntityContainer container = new EdmEntityContainer("NS", "Default"); model.AddElement(container); container.AddEntitySet("Customers", customer); container.AddEntitySet("Orders", order); container.AddEntitySet("People", person); return(_edmModel = model); }
public static IEdmModel GetEdmModel() { if (_edmModel != null) { return(_edmModel); } EdmModel model = new EdmModel(); // entity type 'Customer' with single alternate keys EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); var ssn = customer.AddStructuralProperty("SSN", EdmPrimitiveTypeKind.String); model.AddAlternateKeyAnnotation(customer, new Dictionary <string, IEdmProperty> { { "SSN", ssn } }); model.AddElement(customer); // entity type 'Order' with multiple alternate keys EdmEntityType order = new EdmEntityType("NS", "Order"); order.AddKeys(order.AddStructuralProperty("OrderId", EdmPrimitiveTypeKind.Int32)); var orderName = order.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); var orderToken = order.AddStructuralProperty("Token", EdmPrimitiveTypeKind.Guid); order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32); model.AddAlternateKeyAnnotation(order, new Dictionary <string, IEdmProperty> { { "Name", orderName } }); model.AddAlternateKeyAnnotation(order, new Dictionary <string, IEdmProperty> { { "Token", orderToken } }); model.AddElement(order); // entity type 'Person' with composed alternate keys EdmEntityType person = new EdmEntityType("NS", "Person"); person.AddKeys(person.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); var countryRegion = person.AddStructuralProperty("Country_Region", EdmPrimitiveTypeKind.String); var passport = person.AddStructuralProperty("Passport", EdmPrimitiveTypeKind.String); model.AddAlternateKeyAnnotation(person, new Dictionary <string, IEdmProperty> { { "Country_Region", countryRegion }, { "Passport", passport } }); model.AddElement(person); // complex type address EdmComplexType address = new EdmComplexType("NS", "Address"); var street = address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); var city = address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); model.AddElement(address); // entity type 'Company' with complex type alternate keys EdmEntityType company = new EdmEntityType("NS", "Company"); company.AddKeys(company.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); company.AddStructuralProperty("Location", new EdmComplexTypeReference(address, isNullable: true)); model.AddAlternateKeyAnnotation(company, new Dictionary <string, IEdmProperty> { { "City", city }, { "Street", street } }); model.AddElement(company); // entity sets EdmEntityContainer container = new EdmEntityContainer("NS", "Default"); model.AddElement(container); container.AddEntitySet("Customers", customer); container.AddEntitySet("Orders", order); container.AddEntitySet("People", person); container.AddEntitySet("Companies", company); return(_edmModel = model); }