public EdmParModel() { model = new EdmModel(); person = new EdmEntityType("NS", "Person"); pet = new EdmEntityType("NS", "Pet"); model.AddElement(person); model.AddElement(pet); this.personNavPet = person.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "NavPet", Target = pet, TargetMultiplicity = EdmMultiplicity.Many, }); this.personNavPetCon = person.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "NavPetCon", Target = pet, TargetMultiplicity = EdmMultiplicity.ZeroOrOne, ContainsTarget = true, }); this.personNavPetUnknown = person.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "NavPetUnknown", Target = pet, TargetMultiplicity = EdmMultiplicity.ZeroOrOne, }); var container = new EdmEntityContainer("NS", "Con"); model.AddElement(container); personSet = container.AddEntitySet("PersonSet", person); petSet = container.AddEntitySet("PetSet", pet); personSet.AddNavigationTarget(this.personNavPet, petSet); }
private static void AddNavigationBindings(EntitySetConfiguration configuration, EdmEntitySet entitySet, EntitySetLinkBuilderAnnotation linkBuilder, ODataModelBuilder builder, Dictionary <Type, IEdmStructuredType> edmTypeMap, Dictionary <string, EdmEntitySet> edmEntitySetMap) { foreach (EntityTypeConfiguration entity in builder.ThisAndBaseAndDerivedTypes(configuration.EntityType)) { foreach (NavigationPropertyConfiguration navigation in entity.NavigationProperties) { NavigationPropertyBindingConfiguration binding = configuration.FindBinding(navigation); if (binding != null) { EdmEntityType edmEntityType = edmTypeMap[entity.ClrType] as EdmEntityType; IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name); entitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]); NavigationLinkBuilder linkBuilderFunc = configuration.GetNavigationPropertyLink(navigation); if (linkBuilderFunc != null) { linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc); } } } } }
public CustomersModelWithInheritance() { EdmModel model = new EdmModel(); // Enum type simpleEnum EdmEnumType simpleEnum = new EdmEnumType("NS", "SimpleEnum"); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "First", new EdmEnumMemberValue(0))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Second", new EdmEnumMemberValue(1))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Third", new EdmEnumMemberValue(2))); model.AddElement(simpleEnum); // 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("CountryOrRegion", EdmPrimitiveTypeKind.String); model.AddElement(address); // open complex type "Account" EdmComplexType account = new EdmComplexType("NS", "Account", null, false, true); account.AddStructuralProperty("Bank", EdmPrimitiveTypeKind.String); account.AddStructuralProperty("CardNum", EdmPrimitiveTypeKind.Int64); account.AddStructuralProperty("BankAddress", new EdmComplexTypeReference(address, isNullable: true)); model.AddElement(account); EdmComplexType specialAccount = new EdmComplexType("NS", "SpecialAccount", account, false, true); specialAccount.AddStructuralProperty("SpecialCard", EdmPrimitiveTypeKind.String); model.AddElement(specialAccount); // entity type customer EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); IEdmProperty customerName = customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); customer.AddStructuralProperty("SimpleEnum", simpleEnum.ToEdmTypeReference(isNullable: false)); customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true)); customer.AddStructuralProperty("Account", new EdmComplexTypeReference(account, isNullable: true)); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( EdmPrimitiveTypeKind.String, isNullable: true); var city = customer.AddStructuralProperty( "City", primitiveTypeReference, defaultValue: null); model.AddElement(customer); // derived entity type special customer EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer); specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid); specialCustomer.AddStructuralProperty("SpecialAddress", new EdmComplexTypeReference(address, isNullable: true)); model.AddElement(specialCustomer); // entity type order (open entity type) EdmEntityType order = new EdmEntityType("NS", "Order", null, false, true); // EdmEntityType order = new EdmEntityType("NS", "Order"); order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); order.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32); model.AddElement(order); // derived entity type special order EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order, false, true); specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid); model.AddElement(specialOrder); // test entity EdmEntityType testEntity = new EdmEntityType("Microsoft.AspNet.OData.Test.Query.Expressions", "TestEntity"); testEntity.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Binary); model.AddElement(testEntity); // containment // my order EdmEntityType myOrder = new EdmEntityType("NS", "MyOrder"); myOrder.AddKeys(myOrder.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); myOrder.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); model.AddElement(myOrder); // order line EdmEntityType orderLine = new EdmEntityType("NS", "OrderLine"); orderLine.AddKeys(orderLine.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); orderLine.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); model.AddElement(orderLine); EdmNavigationProperty orderLinesNavProp = myOrder.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "OrderLines", TargetMultiplicity = EdmMultiplicity.Many, Target = orderLine, ContainsTarget = true, }); EdmNavigationProperty nonContainedOrderLinesNavProp = myOrder.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "NonContainedOrderLines", TargetMultiplicity = EdmMultiplicity.Many, Target = orderLine, ContainsTarget = false, }); EdmAction tag = new EdmAction("NS", "tag", returnType: null, isBound: true, entitySetPathExpression: null); tag.AddParameter("entity", new EdmEntityTypeReference(orderLine, false)); model.AddElement(tag); // entity sets EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance"); model.AddElement(container); EdmEntitySet customers = container.AddEntitySet("Customers", customer); EdmEntitySet orders = container.AddEntitySet("Orders", order); EdmEntitySet myOrders = container.AddEntitySet("MyOrders", myOrder); // singletons EdmSingleton vipCustomer = container.AddSingleton("VipCustomer", customer); EdmSingleton mary = container.AddSingleton("Mary", customer); EdmSingleton rootOrder = container.AddSingleton("RootOrder", order); // annotations model.SetOptimisticConcurrencyAnnotation(customers, new[] { city }); // containment IEdmContainedEntitySet orderLines = (IEdmContainedEntitySet)myOrders.FindNavigationTarget(orderLinesNavProp); // no-containment IEdmNavigationSource nonContainedOrderLines = myOrders.FindNavigationTarget(nonContainedOrderLinesNavProp); // actions EdmAction upgrade = new EdmAction("NS", "upgrade", returnType: null, isBound: true, entitySetPathExpression: null); upgrade.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(upgrade); EdmAction specialUpgrade = new EdmAction("NS", "specialUpgrade", returnType: null, isBound: true, entitySetPathExpression: null); specialUpgrade.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(specialUpgrade); // actions bound to collection EdmAction upgradeAll = new EdmAction("NS", "UpgradeAll", returnType: null, isBound: true, entitySetPathExpression: null); upgradeAll.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(customer, false)))); model.AddElement(upgradeAll); EdmAction upgradeSpecialAll = new EdmAction("NS", "UpgradeSpecialAll", returnType: null, isBound: true, entitySetPathExpression: null); upgradeSpecialAll.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(specialCustomer, false)))); model.AddElement(upgradeSpecialAll); // functions IEdmTypeReference returnType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false); IEdmTypeReference stringType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false); IEdmTypeReference intType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false); EdmFunction IsUpgraded = new EdmFunction( "NS", "IsUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); IsUpgraded.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(IsUpgraded); EdmFunction orderByCityAndAmount = new EdmFunction( "NS", "OrderByCityAndAmount", stringType, isBound: true, entitySetPathExpression: null, isComposable: false); orderByCityAndAmount.AddParameter("entity", new EdmEntityTypeReference(customer, false)); orderByCityAndAmount.AddParameter("city", stringType); orderByCityAndAmount.AddParameter("amount", intType); model.AddElement(orderByCityAndAmount); EdmFunction getOrders = new EdmFunction( "NS", "GetOrders", EdmCoreModel.GetCollection(order.ToEdmTypeReference(false)), isBound: true, entitySetPathExpression: null, isComposable: true); getOrders.AddParameter("entity", new EdmEntityTypeReference(customer, false)); getOrders.AddParameter("parameter", intType); model.AddElement(getOrders); EdmFunction IsSpecialUpgraded = new EdmFunction( "NS", "IsSpecialUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); IsSpecialUpgraded.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(IsSpecialUpgraded); EdmFunction getSalary = new EdmFunction( "NS", "GetSalary", stringType, isBound: true, entitySetPathExpression: null, isComposable: false); getSalary.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(getSalary); getSalary = new EdmFunction( "NS", "GetSalary", stringType, isBound: true, entitySetPathExpression: null, isComposable: false); getSalary.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(getSalary); EdmFunction IsAnyUpgraded = new EdmFunction( "NS", "IsAnyUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); EdmCollectionType edmCollectionType = new EdmCollectionType(new EdmEntityTypeReference(customer, false)); IsAnyUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(edmCollectionType)); model.AddElement(IsAnyUpgraded); EdmFunction isCustomerUpgradedWithParam = new EdmFunction( "NS", "IsUpgradedWithParam", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isCustomerUpgradedWithParam.AddParameter("entity", new EdmEntityTypeReference(customer, false)); isCustomerUpgradedWithParam.AddParameter("city", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false)); model.AddElement(isCustomerUpgradedWithParam); EdmFunction isCustomerLocal = new EdmFunction( "NS", "IsLocal", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isCustomerLocal.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(isCustomerLocal); EdmFunction entityFunction = new EdmFunction( "NS", "GetCustomer", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); entityFunction.AddParameter("entity", new EdmEntityTypeReference(customer, false)); entityFunction.AddParameter("customer", new EdmEntityTypeReference(customer, false)); model.AddElement(entityFunction); EdmFunction getOrder = new EdmFunction( "NS", "GetOrder", order.ToEdmTypeReference(false), isBound: true, entitySetPathExpression: null, isComposable: true); // Composable getOrder.AddParameter("entity", new EdmEntityTypeReference(customer, false)); getOrder.AddParameter("orderId", intType); model.AddElement(getOrder); // functions bound to collection EdmFunction isAllUpgraded = new EdmFunction("NS", "IsAllUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isAllUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(customer, false)))); isAllUpgraded.AddParameter("param", intType); model.AddElement(isAllUpgraded); EdmFunction isSpecialAllUpgraded = new EdmFunction("NS", "IsSpecialAllUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isSpecialAllUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(specialCustomer, false)))); isSpecialAllUpgraded.AddParameter("param", intType); model.AddElement(isSpecialAllUpgraded); // navigation properties EdmNavigationProperty ordersNavProp = customer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Orders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); mary.AddNavigationTarget(ordersNavProp, orders); vipCustomer.AddNavigationTarget(ordersNavProp, orders); customers.AddNavigationTarget(ordersNavProp, orders); orders.AddNavigationTarget( order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Customer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, Target = customer }), customers); // navigation properties on derived types. EdmNavigationProperty specialOrdersNavProp = specialCustomer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "SpecialOrders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); vipCustomer.AddNavigationTarget(specialOrdersNavProp, orders); customers.AddNavigationTarget(specialOrdersNavProp, orders); orders.AddNavigationTarget( specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "SpecialCustomer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, Target = customer }), customers); model.SetAnnotationValue <BindableOperationFinder>(model, new BindableOperationFinder(model)); // set properties Model = model; Container = container; Customer = customer; Order = order; Address = address; Account = account; SpecialCustomer = specialCustomer; SpecialOrder = specialOrder; Orders = orders; Customers = customers; VipCustomer = vipCustomer; Mary = mary; RootOrder = rootOrder; OrderLine = orderLine; OrderLines = orderLines; NonContainedOrderLines = nonContainedOrderLines; UpgradeCustomer = upgrade; UpgradeSpecialCustomer = specialUpgrade; CustomerName = customerName; IsCustomerUpgraded = isCustomerUpgradedWithParam; IsSpecialCustomerUpgraded = IsSpecialUpgraded; Tag = tag; }
private IEdmModel GetModel() { EdmModel 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 order = new EdmEntityType("MyNS", "Order"); order.AddKeys(order.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); order.AddStructuralProperty("ShippingAddress", shippingAddressReference); myModel.AddElement(order); EdmEntityType person = new EdmEntityType("MyNS", "Person"); myModel.AddElement(person); customer = new EdmEntityType("MyNS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); customer.AddStructuralProperty("ContactName", EdmPrimitiveTypeKind.String); EdmNavigationPropertyInfo orderLinks = new EdmNavigationPropertyInfo { Name = "Orders", Target = order, TargetMultiplicity = EdmMultiplicity.Many }; EdmNavigationPropertyInfo personLinks = new EdmNavigationPropertyInfo { Name = "Parent", Target = person, TargetMultiplicity = EdmMultiplicity.Many }; customer.AddUnidirectionalNavigation(orderLinks); customer.AddUnidirectionalNavigation(personLinks); myModel.AddElement(customer); EdmEntityType product = new EdmEntityType("MyNS", "Product"); product.AddKeys(product.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); product.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); myModel.AddElement(product); EdmEntityType productDetail = new EdmEntityType("MyNS", "ProductDetail"); productDetail.AddKeys(productDetail.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); productDetail.AddStructuralProperty("Detail", EdmPrimitiveTypeKind.String); myModel.AddElement(productDetail); product.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Details", Target = productDetail, TargetMultiplicity = EdmMultiplicity.Many, ContainsTarget = true, }); EdmNavigationProperty favouriteProducts = customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "FavouriteProducts", Target = product, TargetMultiplicity = EdmMultiplicity.Many, }); EdmNavigationProperty productBeingViewed = customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "ProductBeingViewed", Target = product, TargetMultiplicity = EdmMultiplicity.ZeroOrOne, }); EdmEntityContainer container = new EdmEntityContainer("MyNS", "Example30"); customers = container.AddEntitySet("Customers", customer); container.AddEntitySet("Orders", order); EdmEntitySet products = container.AddEntitySet("Products", product); customers.AddNavigationTarget(favouriteProducts, products); customers.AddNavigationTarget(productBeingViewed, products); myModel.AddElement(container); return(myModel); }
private static IEdmModel GetEdmModel() { EdmModel model = new EdmModel(); // Order EdmEntityType order = new EdmEntityType("NS", "Order", null, false, true); order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); // Customer EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); model.AddElement(customer); // VipCustomer EdmEntityType vipCustomer = new EdmEntityType("NS", "VipCustomer", customer); model.AddElement(vipCustomer); EdmEntityContainer container = new EdmEntityContainer("NS", "Default"); EdmEntitySet orders = container.AddEntitySet("Orders", order); EdmEntitySet customers = container.AddEntitySet("Customers", customer); EdmSingleton me = container.AddSingleton("Me", customer); model.AddElement(container); EdmNavigationProperty ordersNavProp = customer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Orders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); customers.AddNavigationTarget(ordersNavProp, orders); me.AddNavigationTarget(ordersNavProp, orders); EdmNavigationProperty orderNavProp = customer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Order", TargetMultiplicity = EdmMultiplicity.One, Target = order }); customers.AddNavigationTarget(orderNavProp, orders); me.AddNavigationTarget(orderNavProp, orders); EdmNavigationProperty subOrdersNavProp = customer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "SubOrders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); customers.AddNavigationTarget(subOrdersNavProp, orders, new EdmPathExpression("NS.VipCustomer/SubOrders")); me.AddNavigationTarget(subOrdersNavProp, orders, new EdmPathExpression("NS.VipCustomer/SubOrders")); EdmNavigationProperty subOrderNavProp = customer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "SubOrder", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); customers.AddNavigationTarget(subOrderNavProp, orders, new EdmPathExpression("NS.VipCustomer/SubOrder")); me.AddNavigationTarget(subOrderNavProp, orders, new EdmPathExpression("NS.VipCustomer/SubOrder")); return(model); }
public static IEdmModel BuildEdmModel(string containerNamespace, string containerName, IEnumerable <IStructuralTypeConfiguration> entityTypeConfigurations, IEnumerable <IEntitySetConfiguration> entitySetConfigurations) { if (entityTypeConfigurations == null) { throw Error.ArgumentNull("entityTypeConfigurations"); } if (entitySetConfigurations == null) { throw Error.ArgumentNull("entitySetConfigurations"); } EdmModel model = new EdmModel(); Dictionary <string, IEdmStructuredType> types = EdmTypeBuilder.GetTypes(entityTypeConfigurations) .OfType <IEdmStructuredType>() .ToDictionary(t => t.ToString()); foreach (IEdmStructuredType type in types.Values) { if (type.TypeKind == EdmTypeKind.Complex) { model.AddElement(type as IEdmComplexType); } else if (type.TypeKind == EdmTypeKind.Entity) { model.AddElement(type as IEdmEntityType); } else { throw Error.InvalidOperation(SRResources.UnsupportedEntityTypeInModel); } } if (entitySetConfigurations.Any()) { EdmEntityContainer container = new EdmEntityContainer(containerNamespace, containerName); Dictionary <string, EdmEntitySet> edmEntitySetMap = new Dictionary <string, EdmEntitySet>(); foreach (IEntitySetConfiguration entitySet in entitySetConfigurations) { EdmEntitySet edmEntitySet = container.AddEntitySet(entitySet.Name, (IEdmEntityType)types[entitySet.EntityType.FullName]); EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(entitySet); model.SetEntitySetLinkBuilderAnnotation(edmEntitySet, entitySetLinkBuilderAnnotation); model.SetAnnotationValue <EntitySetUrlAnnotation>(edmEntitySet, new EntitySetUrlAnnotation { Url = entitySet.GetUrl() }); edmEntitySetMap.Add(edmEntitySet.Name, edmEntitySet); } foreach (IEntitySetConfiguration entitySet in entitySetConfigurations) { EdmEntitySet edmEntitySet = edmEntitySetMap[entitySet.Name]; EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = model.GetEntitySetLinkBuilder(edmEntitySet) as EntitySetLinkBuilderAnnotation; foreach (NavigationPropertyConfiguration navigation in entitySet.EntityType.NavigationProperties) { NavigationPropertyBinding binding = entitySet.FindBinding(navigation); if (binding != null) { EdmEntityType edmEntityType = types[entitySet.EntityType.FullName] as EdmEntityType; IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name); edmEntitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]); entitySetLinkBuilderAnnotation.AddNavigationPropertyLinkBuilder(edmNavigationProperty, entitySet.GetNavigationPropertyLink(edmNavigationProperty.Name)); } } } model.AddElement(container); } return(model); }
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); }
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); }
public EdmModel BuildEdmModel(params IEdmModel[] refModels) { AddOperations(); Dictionary <Type, EntityTypeInfo> entityTypeInfos = BuildEntityTypes(refModels); foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { if (!typeInfo.IsRefModel) { typeInfo.BuildProperties(entityTypeInfos, _enumTypes, _complexTypes); } } foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { if (!typeInfo.IsRefModel) { foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties) { fkeyInfo.BuildNavigationProperty(); } } } var edmModel = new EdmModel(false); edmModel.AddElements(_enumTypes.Values); foreach (KeyValuePair <Type, EdmEnumType> enumType in _enumTypes) { edmModel.SetClrType(enumType.Value, enumType.Key); } edmModel.AddElements(_complexTypes.Values); foreach (KeyValuePair <Type, EdmComplexType> complexType in _complexTypes) { edmModel.SetClrType(complexType.Value, complexType.Key); } var container = new EdmEntityContainer(_dataAdapter.DataContextType.Namespace, _dataAdapter.DataContextType.Name); edmModel.SetDataAdapter(container, _dataAdapter); var entitySets = new Dictionary <IEdmEntityType, EdmEntitySet>(entityTypeInfos.Count); foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { if (!typeInfo.IsRefModel) { edmModel.AddElement(typeInfo.EdmType); edmModel.SetClrType(typeInfo.EdmType, typeInfo.ClrType); } Db.OeEntitySetAdapter entitySetAdapter = _dataAdapter.EntitySetAdapters.Find(typeInfo.ClrType); if (entitySetAdapter != null) { EdmEntitySet entitySet = container.AddEntitySet(entitySetAdapter.EntitySetName, typeInfo.EdmType); edmModel.SetEntitySetAdapter(entitySet, entitySetAdapter); entitySets.Add(typeInfo.EdmType, entitySet); } } var manyToManyBuilder = new ManyToManyBuilder(edmModel, _metadataProvider, entityTypeInfos); foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { if (!typeInfo.IsRefModel) { foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties) { EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType]; EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType]; if (fkeyInfo.DependentNavigationProperty == null) { principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, dependent); } else { dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal); if (fkeyInfo.EdmNavigationProperty.Partner != null) { principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent); } } } manyToManyBuilder.Build(typeInfo); } } foreach (OeOperationConfiguration operationConfiguration in _operationConfigurations) { if (operationConfiguration.IsEdmFunction) { EdmFunction edmFunction = BuildFunction(operationConfiguration, entityTypeInfos); edmModel.AddElement(edmFunction); if (edmFunction.IsBound) { edmModel.SetMethodInfo(edmFunction, operationConfiguration.MethodInfo); } else { container.AddFunctionImport(operationConfiguration.ImportName, edmFunction, edmFunction.EntitySetPath); edmModel.SetIsDbFunction(edmFunction, operationConfiguration.IsDbFunction); } } else { EdmAction edmAction = BuildAction(operationConfiguration, entityTypeInfos); edmModel.AddElement(edmAction); container.AddActionImport(operationConfiguration.ImportName, edmAction); edmModel.SetIsDbFunction(edmAction, operationConfiguration.IsDbFunction); } } edmModel.AddElement(container); _dataAdapter.SetEdmModel(edmModel); foreach (IEdmModel refModel in refModels) { edmModel.AddReferencedModel(refModel); } return(edmModel); }
public static IEdmModel CreateODataServiceModel(string ns) { EdmModel model = new EdmModel(); var defaultContainer = new EdmEntityContainer(ns, "OperationService"); model.AddElement(defaultContainer); #region ComplexType var addressType = new EdmComplexType(ns, "Address"); addressType.AddProperty(new EdmStructuralProperty(addressType, "Street", EdmCoreModel.Instance.GetString(false))); addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false))); addressType.AddProperty(new EdmStructuralProperty(addressType, "PostalCode", EdmCoreModel.Instance.GetString(false))); model.AddElement(addressType); var homeAddressType = new EdmComplexType(ns, "HomeAddress", addressType, false); homeAddressType.AddProperty(new EdmStructuralProperty(homeAddressType, "FamilyName", EdmCoreModel.Instance.GetString(true))); model.AddElement(homeAddressType); var companyAddressType = new EdmComplexType(ns, "CompanyAddress", addressType, false); companyAddressType.AddProperty(new EdmStructuralProperty(companyAddressType, "CompanyName", EdmCoreModel.Instance.GetString(false))); model.AddElement(companyAddressType); #endregion #region EnumType var customerLevelType = new EdmEnumType(ns, "CustomerLevel"); customerLevelType.AddMember("Common", new EdmIntegerConstant(0)); customerLevelType.AddMember("Silver", new EdmIntegerConstant(1)); customerLevelType.AddMember("Gold", new EdmIntegerConstant(2)); model.AddElement(customerLevelType); #endregion #region EntityType var customerType = new EdmEntityType(ns, "Customer"); var customerIdProperty = new EdmStructuralProperty(customerType, "ID", EdmCoreModel.Instance.GetInt32(false)); customerType.AddProperty(customerIdProperty); customerType.AddKeys(customerIdProperty); customerType.AddStructuralProperty("FirstName", EdmCoreModel.Instance.GetString(false)); customerType.AddStructuralProperty("LastName", EdmCoreModel.Instance.GetString(false)); customerType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, true)); customerType.AddStructuralProperty("Emails", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true)))); customerType.AddStructuralProperty("Level", new EdmEnumTypeReference(customerLevelType, false)); model.AddElement(customerType); var orderType = new EdmEntityType(ns, "Order"); var orderIdProperty = new EdmStructuralProperty(orderType, "ID", EdmCoreModel.Instance.GetInt32(false)); orderType.AddProperty(orderIdProperty); orderType.AddKeys(orderIdProperty); orderType.AddStructuralProperty("OrderDate", EdmCoreModel.Instance.GetDateTimeOffset(false)); orderType.AddStructuralProperty("Notes", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true)))); model.AddElement(orderType); var ordersNavigation = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many }); var customerForOrderNavigation = orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Customer", Target = customerType, TargetMultiplicity = EdmMultiplicity.One }); #endregion #region EntitySet var customerSet = new EdmEntitySet(defaultContainer, "Customers", customerType); defaultContainer.AddElement(customerSet); var orderSet = new EdmEntitySet(defaultContainer, "Orders", orderType); defaultContainer.AddElement(orderSet); customerSet.AddNavigationTarget(ordersNavigation, orderSet); orderSet.AddNavigationTarget(customerForOrderNavigation, customerSet); #endregion #region Operations //UnBound Action: ResetDataSource var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null); model.AddElement(resetDataSourceAction); defaultContainer.AddActionImport(resetDataSourceAction); var customerTypeReference = new EdmEntityTypeReference(customerType, true); var customerCollectionTypeReference = new EdmCollectionTypeReference(new EdmCollectionType(customerTypeReference)); var addressTypeReference = new EdmComplexTypeReference(addressType, true); var addressCollectionTypeRefernce = new EdmCollectionTypeReference(new EdmCollectionType(addressTypeReference)); var stringTypeReference = EdmCoreModel.Instance.GetString(true); var stringCollectionTypeReference = new EdmCollectionTypeReference(new EdmCollectionType(stringTypeReference)); var orderTypeReference = new EdmEntityTypeReference(orderType, true); var orderCollectionTypeReference = new EdmCollectionTypeReference(new EdmCollectionType(orderTypeReference)); //Bound Function : Bound to Collection Of Entity, Parameter is Complex Value, Return Entity. var getCustomerForAddressFunction = new EdmFunction(ns, "GetCustomerForAddress", customerTypeReference, true, new EdmPathExpression("customers"), true); getCustomerForAddressFunction.AddParameter("customers", customerCollectionTypeReference); getCustomerForAddressFunction.AddParameter("address", addressTypeReference); model.AddElement(getCustomerForAddressFunction); //Bound Function : Bound to Collection Of Entity, Parameter is Collection of Complex Value, Return Collection Of Entity. var getCustomersForAddressesFunction = new EdmFunction(ns, "GetCustomersForAddresses", customerCollectionTypeReference, true, new EdmPathExpression("customers"), true); getCustomersForAddressesFunction.AddParameter("customers", customerCollectionTypeReference); getCustomersForAddressesFunction.AddParameter("addresses", addressCollectionTypeRefernce); model.AddElement(getCustomersForAddressesFunction); //Bound Function : Bound to Collection Of Entity, Parameter is Collection of Complex Value, Return Entity var getCustomerForAddressesFunction = new EdmFunction(ns, "GetCustomerForAddresses", customerTypeReference, true, new EdmPathExpression("customers"), true); getCustomerForAddressesFunction.AddParameter("customers", customerCollectionTypeReference); getCustomerForAddressesFunction.AddParameter("addresses", addressCollectionTypeRefernce); model.AddElement(getCustomerForAddressesFunction); //Bound Function : Bound to Entity, Return Complex Value var getCustomerAddressFunction = new EdmFunction(ns, "GetCustomerAddress", addressTypeReference, true, null, true); getCustomerAddressFunction.AddParameter("customer", customerTypeReference); model.AddElement(getCustomerAddressFunction); //Bound Function : Bound to Entity, Parameter is Complex Value, Return Entity var verifyCustomerAddressFunction = new EdmFunction(ns, "VerifyCustomerAddress", customerTypeReference, true, new EdmPathExpression("customer"), true); verifyCustomerAddressFunction.AddParameter("customer", customerTypeReference); verifyCustomerAddressFunction.AddParameter("addresses", addressTypeReference); model.AddElement(verifyCustomerAddressFunction); //Bound Function : Bound to Entity, Parameter is Entity, Return Entity var verifyCustomerByOrderFunction = new EdmFunction(ns, "VerifyCustomerByOrder", customerTypeReference, true, new EdmPathExpression("customer"), true); verifyCustomerByOrderFunction.AddParameter("customer", customerTypeReference); verifyCustomerByOrderFunction.AddParameter("order", orderTypeReference); model.AddElement(verifyCustomerByOrderFunction); //Bound Function : Bound to Entity, Parameter is Collection of String, Return Collection of Entity var getOrdersFromCustomerByNotesFunction = new EdmFunction(ns, "GetOrdersFromCustomerByNotes", orderCollectionTypeReference, true, new EdmPathExpression("customer/Orders"), true); getOrdersFromCustomerByNotesFunction.AddParameter("customer", customerTypeReference); getOrdersFromCustomerByNotesFunction.AddParameter("notes", stringCollectionTypeReference); model.AddElement(getOrdersFromCustomerByNotesFunction); //Bound Function : Bound to Collection Of Entity, Parameter is String, Return Collection of Entity var getOrdersByNoteFunction = new EdmFunction(ns, "GetOrdersByNote", orderCollectionTypeReference, true, new EdmPathExpression("orders"), true); getOrdersByNoteFunction.AddParameter("orders", orderCollectionTypeReference); getOrdersByNoteFunction.AddParameter("note", stringTypeReference); model.AddElement(getOrdersByNoteFunction); //Bound Function : Bound to Collection Of Entity, Parameter is Collection of String, Return Entity var getOrderByNoteFunction = new EdmFunction(ns, "GetOrderByNote", orderTypeReference, true, new EdmPathExpression("orders"), true); getOrderByNoteFunction.AddParameter("orders", orderCollectionTypeReference); getOrderByNoteFunction.AddParameter("notes", stringCollectionTypeReference); model.AddElement(getOrderByNoteFunction); //Bound Function : Bound to Collection Of Entity, Parameter is Collection of Entity, Return Collection Of Entity var getCustomersByOrdersFunction = new EdmFunction(ns, "GetCustomersByOrders", customerCollectionTypeReference, true, new EdmPathExpression("customers"), true); getCustomersByOrdersFunction.AddParameter("customers", customerCollectionTypeReference); getCustomersByOrdersFunction.AddParameter("orders", orderCollectionTypeReference); model.AddElement(getCustomersByOrdersFunction); //Bound Function : Bound to Collection Of Entity, Parameter is Entity, Return Entity var getCustomerByOrderFunction = new EdmFunction(ns, "GetCustomerByOrder", customerTypeReference, true, new EdmPathExpression("customers"), true); getCustomerByOrderFunction.AddParameter("customers", customerCollectionTypeReference); getCustomerByOrderFunction.AddParameter("order", orderTypeReference); model.AddElement(getCustomerByOrderFunction); // Function Import: Parameter is Collection of Entity, Return Collection Of Entity var getCustomersByOrdersUnboundFunction = new EdmFunction(ns, "GetCustomersByOrders", customerCollectionTypeReference, false, null, true); getCustomersByOrdersUnboundFunction.AddParameter("orders", orderCollectionTypeReference); model.AddElement(getCustomersByOrdersUnboundFunction); defaultContainer.AddFunctionImport(getCustomersByOrdersUnboundFunction.Name, getCustomersByOrdersUnboundFunction, new EdmEntitySetReferenceExpression(customerSet)); // Function Import: Parameter is Collection of Entity, Return Entity var getCustomerByOrderUnboundFunction = new EdmFunction(ns, "GetCustomerByOrder", customerTypeReference, false, null, true); getCustomerByOrderUnboundFunction.AddParameter("order", orderTypeReference); model.AddElement(getCustomerByOrderUnboundFunction); defaultContainer.AddFunctionImport(getCustomerByOrderUnboundFunction.Name, getCustomerByOrderUnboundFunction, new EdmEntitySetReferenceExpression(customerSet)); // Function Import: Bound to Entity, Return Complex Value var getCustomerAddressUnboundFunction = new EdmFunction(ns, "GetCustomerAddress", addressTypeReference, false, null, true); getCustomerAddressUnboundFunction.AddParameter("customer", customerTypeReference); model.AddElement(getCustomerAddressUnboundFunction); defaultContainer.AddFunctionImport(getCustomerAddressUnboundFunction.Name, getCustomerAddressUnboundFunction); #endregion IEnumerable <EdmError> errors; model.Validate(out errors); if (errors.Any()) { throw new SystemException("The model is not valid, please correct it"); } return(model); }
public static IEdmModel GetEdmModel1() { EdmModel model = new EdmModel(); // Complex Type EdmComplexType address = new EdmComplexType("WebApiDocNS", "Address"); address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); model.AddElement(address); EdmComplexType subAddress = new EdmComplexType("WebApiDocNS", "SubAddress", address); subAddress.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); model.AddElement(subAddress); // Enum type EdmEnumType color = new EdmEnumType("WebApiDocNS", "Color"); color.AddMember(new EdmEnumMember(color, "Red", new EdmEnumMemberValue(0))); color.AddMember(new EdmEnumMember(color, "Blue", new EdmEnumMemberValue(1))); color.AddMember(new EdmEnumMember(color, "Green", new EdmEnumMemberValue(2))); model.AddElement(color); // Entity type EdmEntityType customer = new EdmEntityType("WebApiDocNS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32)); customer.AddStructuralProperty("Location", new EdmComplexTypeReference(address, isNullable: true)); model.AddElement(customer); EdmEntityType vipCustomer = new EdmEntityType("WebApiDocNS", "VipCustomer", customer); vipCustomer.AddStructuralProperty("FavoriteColor", new EdmEnumTypeReference(color, isNullable: false)); model.AddElement(vipCustomer); EdmEntityType order = new EdmEntityType("WebApiDocNS", "Order"); order.AddKeys(order.AddStructuralProperty("OrderId", EdmPrimitiveTypeKind.Int32)); order.AddStructuralProperty("Token", EdmPrimitiveTypeKind.Guid); model.AddElement(order); EdmEntityContainer container = new EdmEntityContainer("WebApiDocNS", "Container"); EdmEntitySet customers = container.AddEntitySet("Customers", customer); EdmEntitySet orders = container.AddEntitySet("Orders", order); model.AddElement(container); // EdmSingleton mary = container.AddSingleton("Mary", customer); // navigation properties EdmNavigationProperty ordersNavProp = customer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Orders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); customers.AddNavigationTarget(ordersNavProp, orders); // function IEdmTypeReference stringType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false); IEdmTypeReference intType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false); EdmFunction getFirstName = new EdmFunction("WebApiDocNS", "GetFirstName", stringType, isBound: true, entitySetPathExpression: null, isComposable: false); getFirstName.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(getFirstName); EdmFunction getNumber = new EdmFunction("WebApiDocNS", "GetOrderCount", intType, isBound: false, entitySetPathExpression: null, isComposable: false); model.AddElement(getNumber); container.AddFunctionImport("GetOrderCount", getNumber); // action EdmAction calculate = new EdmAction("WebApiDocNS", "CalculateOrderPrice", returnType: null, isBound: true, entitySetPathExpression: null); calculate.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(calculate); EdmAction change = new EdmAction("WebApiDocNS", "ChangeCustomerById", returnType: null, isBound: false, entitySetPathExpression: null); change.AddParameter("Id", intType); model.AddElement(change); container.AddActionImport("ChangeCustomerById", change); return(model); }
public static IEdmModel CreateServiceEdmModel(string ns) { EdmModel model = new EdmModel(); var defaultContainer = new EdmEntityContainer(ns, "PerfInMemoryContainer"); model.AddElement(defaultContainer); var personType = new EdmEntityType(ns, "Person"); var personIdProperty = new EdmStructuralProperty(personType, "PersonID", EdmCoreModel.Instance.GetInt32(false)); personType.AddProperty(personIdProperty); personType.AddKeys(new IEdmStructuralProperty[] { personIdProperty }); personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", EdmCoreModel.Instance.GetString(false))); personType.AddProperty(new EdmStructuralProperty(personType, "LastName", EdmCoreModel.Instance.GetString(false))); personType.AddProperty(new EdmStructuralProperty(personType, "MiddleName", EdmCoreModel.Instance.GetString(true))); personType.AddProperty(new EdmStructuralProperty(personType, "Age", EdmCoreModel.Instance.GetInt32(false))); model.AddElement(personType); var simplePersonSet = new EdmEntitySet(defaultContainer, "SimplePeopleSet", personType); defaultContainer.AddElement(simplePersonSet); var largetPersonSet = new EdmEntitySet(defaultContainer, "LargePeopleSet", personType); defaultContainer.AddElement(largetPersonSet); var addressType = new EdmComplexType(ns, "Address"); addressType.AddProperty(new EdmStructuralProperty(addressType, "Street", EdmCoreModel.Instance.GetString(false))); addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false))); addressType.AddProperty(new EdmStructuralProperty(addressType, "PostalCode", EdmCoreModel.Instance.GetString(false))); model.AddElement(addressType); var companyType = new EdmEntityType(ns, "Company"); var companyId = new EdmStructuralProperty(companyType, "CompanyID", EdmCoreModel.Instance.GetInt32(false)); companyType.AddProperty(companyId); companyType.AddKeys(companyId); companyType.AddProperty(new EdmStructuralProperty(companyType, "Name", EdmCoreModel.Instance.GetString(true))); companyType.AddProperty(new EdmStructuralProperty(companyType, "Address", new EdmComplexTypeReference(addressType, true))); companyType.AddProperty(new EdmStructuralProperty(companyType, "Revenue", EdmCoreModel.Instance.GetInt32(false))); model.AddElement(companyType); var companySet = new EdmEntitySet(defaultContainer, "CompanySet", companyType); defaultContainer.AddElement(companySet); var companyEmployeeNavigation = companyType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Employees", Target = personType, TargetMultiplicity = EdmMultiplicity.Many }); companySet.AddNavigationTarget(companyEmployeeNavigation, largetPersonSet); // ResetDataSource var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null); model.AddElement(resetDataSourceAction); defaultContainer.AddActionImport(resetDataSourceAction); return(model); }
private void PairUpNavigationPropertyWithResourceAssociationSet(MetadataProviderEdmEntityContainer entityContainer, ResourceAssociationSet resourceAssociationSet, ResourceAssociationType resourceAssociationType, ResourceType resourceType, ResourceProperty navigationProperty) { string associationEndName; EdmMultiplicity multiplicity; EdmOnDeleteAction deleteBehavior; string str3; EdmMultiplicity multiplicity2; EdmOnDeleteAction none; string typeNamespace; bool flag = (resourceAssociationSet.End1.ResourceProperty != null) && (resourceAssociationSet.End2.ResourceProperty != null); string entitySetName = MetadataProviderUtils.GetEntitySetName(resourceAssociationSet.End1.ResourceSet); string name = MetadataProviderUtils.GetEntitySetName(resourceAssociationSet.End2.ResourceSet); bool isPrinciple = false; List <IEdmStructuralProperty> dependentProperties = null; if (resourceAssociationType != null) { associationEndName = resourceAssociationType.End1.Name; deleteBehavior = resourceAssociationType.End1.DeleteBehavior; multiplicity2 = MetadataProviderUtils.ConvertMultiplicity(resourceAssociationType.End1.Multiplicity); str3 = resourceAssociationType.End2.Name; none = resourceAssociationType.End2.DeleteBehavior; multiplicity = MetadataProviderUtils.ConvertMultiplicity(resourceAssociationType.End2.Multiplicity); ResourceReferentialConstraint referentialConstraint = resourceAssociationType.ReferentialConstraint; if (referentialConstraint != null) { isPrinciple = object.ReferenceEquals(resourceAssociationType.End1, referentialConstraint.PrincipalEnd); IEdmEntityType type = isPrinciple ? ((IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End2.ResourceType)) : ((IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End1.ResourceType)); dependentProperties = new List <IEdmStructuralProperty>(); foreach (ResourceProperty property in referentialConstraint.DependentProperties) { IEdmProperty property2 = type.FindProperty(property.Name); dependentProperties.Add((IEdmStructuralProperty)property2); } } } else { if (!flag) { if (resourceAssociationSet.End1.ResourceProperty != null) { associationEndName = resourceType.Name; str3 = navigationProperty.Name; } else { associationEndName = navigationProperty.Name; str3 = resourceType.Name; } } else { associationEndName = MetadataProviderUtils.GetAssociationEndName(resourceAssociationSet.End1.ResourceType, resourceAssociationSet.End1.ResourceProperty); str3 = MetadataProviderUtils.GetAssociationEndName(resourceAssociationSet.End2.ResourceType, resourceAssociationSet.End2.ResourceProperty); } multiplicity = MetadataProviderUtils.GetMultiplicity(resourceAssociationSet.End1.ResourceProperty); deleteBehavior = EdmOnDeleteAction.None; multiplicity2 = MetadataProviderUtils.GetMultiplicity(resourceAssociationSet.End2.ResourceProperty); none = EdmOnDeleteAction.None; } string associationName = (resourceAssociationType == null) ? MetadataProviderUtils.GetAssociationName(resourceAssociationSet) : resourceAssociationType.Name; if ((resourceAssociationType == null) || (resourceAssociationType.NamespaceName == null)) { ResourceAssociationSetEnd end = (resourceAssociationSet.End1.ResourceProperty != null) ? resourceAssociationSet.End1 : resourceAssociationSet.End2; typeNamespace = this.GetTypeNamespace(end.ResourceType); } else { typeNamespace = resourceAssociationType.NamespaceName; } ResourceProperty resourceProperty = resourceAssociationSet.End1.ResourceProperty; ResourceProperty property4 = resourceAssociationSet.End2.ResourceProperty; MetadataProviderEdmNavigationProperty partnerProperty = null; MetadataProviderEdmNavigationProperty property6 = null; if (resourceProperty != null) { IEdmEntityType type2 = (IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End1.ResourceType); partnerProperty = (MetadataProviderEdmNavigationProperty)type2.FindProperty(resourceProperty.Name); } if (property4 != null) { IEdmEntityType type3 = (IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End2.ResourceType); property6 = (MetadataProviderEdmNavigationProperty)type3.FindProperty(property4.Name); } IEdmNavigationProperty property7 = (partnerProperty != null) ? ((IEdmNavigationProperty)partnerProperty) : ((IEdmNavigationProperty) new MetadataProviderEdmSilentNavigationProperty(property6, deleteBehavior, multiplicity, associationEndName)); IEdmNavigationProperty partner = (property6 != null) ? ((IEdmNavigationProperty)property6) : ((IEdmNavigationProperty) new MetadataProviderEdmSilentNavigationProperty(partnerProperty, none, multiplicity2, str3)); MetadataProviderUtils.FixUpNavigationPropertyWithAssociationSetData(property7, partner, isPrinciple, dependentProperties, deleteBehavior, multiplicity); MetadataProviderUtils.FixUpNavigationPropertyWithAssociationSetData(partner, property7, !isPrinciple, dependentProperties, none, multiplicity2); EdmEntitySet entitySet = (EdmEntitySet)entityContainer.FindEntitySet(entitySetName); EdmEntitySet target = (EdmEntitySet)entityContainer.FindEntitySet(name); if (partnerProperty != null) { entitySet.AddNavigationTarget(partnerProperty, target); this.SetAssociationSetName(entitySet, partnerProperty, resourceAssociationSet.Name); this.SetAssociationSetAnnotations(entitySet, partnerProperty, MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End1.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End2.CustomAnnotations)); } if (property6 != null) { target.AddNavigationTarget(property6, entitySet); this.SetAssociationSetName(target, property6, resourceAssociationSet.Name); if (partnerProperty == null) { this.SetAssociationSetAnnotations(entitySet, property6, MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End1.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End2.CustomAnnotations)); } } this.SetAssociationNamespace(property7, typeNamespace); this.SetAssociationName(property7, associationName); this.SetAssociationEndName(property7, associationEndName); this.SetAssociationNamespace(partner, typeNamespace); this.SetAssociationName(partner, associationName); this.SetAssociationEndName(partner, str3); if (resourceAssociationType != null) { this.SetAssociationAnnotations(property7, MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationType.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, (property7.GetPrimary() == property7) ? resourceAssociationType.End1.CustomAnnotations : resourceAssociationType.End2.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, (property7.GetPrimary() == property7) ? resourceAssociationType.End2.CustomAnnotations : resourceAssociationType.End1.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, (resourceAssociationType.ReferentialConstraint != null) ? resourceAssociationType.ReferentialConstraint.CustomAnnotations : null)); } }
public static IEdmModel CreateModel(string ns) { EdmModel model = new EdmModel(); var defaultContainer = new EdmEntityContainer(ns, "DefaultContainer"); model.AddElement(defaultContainer); var addressType = new EdmComplexType(ns, "Address"); addressType.AddProperty(new EdmStructuralProperty(addressType, "Road", EdmCoreModel.Instance.GetString(false))); addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false))); model.AddElement(addressType); var personType = new EdmEntityType(ns, "Person"); var personIdProperty = new EdmStructuralProperty(personType, "PersonId", EdmCoreModel.Instance.GetInt32(false)); personType.AddProperty(personIdProperty); personType.AddKeys(new IEdmStructuralProperty[] { personIdProperty }); personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", EdmCoreModel.Instance.GetString(false))); personType.AddProperty(new EdmStructuralProperty(personType, "LastName", EdmCoreModel.Instance.GetString(false))); personType.AddProperty(new EdmStructuralProperty(personType, "Address", new EdmComplexTypeReference(addressType, true))); personType.AddProperty(new EdmStructuralProperty(personType, "Descriptions", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(false))))); model.AddElement(personType); var peopleSet = new EdmEntitySet(defaultContainer, "People", personType); defaultContainer.AddElement(peopleSet); var numberComboType = new EdmComplexType(ns, "NumberCombo"); numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Small", EdmCoreModel.Instance.GetInt32(false))); numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Middle", EdmCoreModel.Instance.GetInt64(false))); numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Large", EdmCoreModel.Instance.GetDecimal(false))); model.AddElement(numberComboType); var productType = new EdmEntityType(ns, "Product"); var productIdProperty = new EdmStructuralProperty(productType, "ProductId", EdmCoreModel.Instance.GetInt32(false)); productType.AddProperty(productIdProperty); productType.AddKeys(new IEdmStructuralProperty[] { productIdProperty }); productType.AddProperty(new EdmStructuralProperty(productType, "Quantity", EdmCoreModel.Instance.GetInt64(false))); productType.AddProperty(new EdmStructuralProperty(productType, "LifeTimeInSeconds", EdmCoreModel.Instance.GetDecimal(false))); productType.AddProperty(new EdmStructuralProperty(productType, "TheCombo", new EdmComplexTypeReference(numberComboType, true))); productType.AddProperty(new EdmStructuralProperty(productType, "LargeNumbers", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetDecimal(false))))); model.AddElement(productType); var productsSet = new EdmEntitySet(defaultContainer, "Products", productType); defaultContainer.AddElement(productsSet); var productsProperty = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Products", Target = productType, TargetMultiplicity = EdmMultiplicity.Many }); peopleSet.AddNavigationTarget(productsProperty, productsSet); IEnumerable <EdmError> errors; model.Validate(out errors); return(model); }
public static IEdmModel CreateTripPinServiceModel(string ns) { EdmModel model = new EdmModel(); var defaultContainer = new EdmEntityContainer(ns, "DefaultContainer"); model.AddElement(defaultContainer); var genderType = new EdmEnumType(ns, "PersonGender", isFlags: false); genderType.AddMember("Male", new EdmIntegerConstant(0)); genderType.AddMember("Female", new EdmIntegerConstant(1)); genderType.AddMember("Unknown", new EdmIntegerConstant(2)); model.AddElement(genderType); var cityType = new EdmComplexType(ns, "City"); cityType.AddProperty(new EdmStructuralProperty(cityType, "CountryRegion", EdmCoreModel.Instance.GetString(false))); cityType.AddProperty(new EdmStructuralProperty(cityType, "Name", EdmCoreModel.Instance.GetString(false))); cityType.AddProperty(new EdmStructuralProperty(cityType, "Region", EdmCoreModel.Instance.GetString(false))); model.AddElement(cityType); var locationType = new EdmComplexType(ns, "Location", null, false, true); locationType.AddProperty(new EdmStructuralProperty(locationType, "Address", EdmCoreModel.Instance.GetString(false))); locationType.AddProperty(new EdmStructuralProperty(locationType, "City", new EdmComplexTypeReference(cityType, false))); model.AddElement(locationType); var eventLocationType = new EdmComplexType(ns, "EventLocation", locationType, false, true); eventLocationType.AddProperty(new EdmStructuralProperty(eventLocationType, "BuildingInfo", EdmCoreModel.Instance.GetString(true))); model.AddElement(eventLocationType); var airportLocationType = new EdmComplexType(ns, "AirportLocation", locationType, false, true); airportLocationType.AddProperty(new EdmStructuralProperty(airportLocationType, "Loc", EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, false))); model.AddElement(airportLocationType); var photoType = new EdmEntityType(ns, "Photo", null, false, false, true); var photoIdProperty = new EdmStructuralProperty(photoType, "Id", EdmCoreModel.Instance.GetInt64(false)); photoType.AddProperty(photoIdProperty); photoType.AddKeys(photoIdProperty); photoType.AddProperty(new EdmStructuralProperty(photoType, "Name", EdmCoreModel.Instance.GetString(true))); model.AddElement(photoType); var photoSet = new EdmEntitySet(defaultContainer, "Photos", photoType); defaultContainer.AddElement(photoSet); var personType = new EdmEntityType(ns, "Person", null, false, /* isOpen */ true); var personIdProperty = new EdmStructuralProperty(personType, "UserName", EdmCoreModel.Instance.GetString(false)); personType.AddProperty(personIdProperty); personType.AddKeys(personIdProperty); personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", EdmCoreModel.Instance.GetString(false))); personType.AddProperty(new EdmStructuralProperty(personType, "LastName", EdmCoreModel.Instance.GetString(false))); personType.AddProperty(new EdmStructuralProperty(personType, "Emails", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true))))); personType.AddProperty(new EdmStructuralProperty(personType, "AddressInfo", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(locationType, true))))); personType.AddProperty(new EdmStructuralProperty(personType, "Gender", new EdmEnumTypeReference(genderType, true))); personType.AddProperty(new EdmStructuralProperty(personType, "Concurrency", EdmCoreModel.Instance.GetInt64(false))); model.AddElement(personType); var personSet = new EdmEntitySet(defaultContainer, "People", personType); defaultContainer.AddElement(personSet); var airlineType = new EdmEntityType(ns, "Airline"); var airlineCodeProp = new EdmStructuralProperty(airlineType, "AirlineCode", EdmCoreModel.Instance.GetString(false)); airlineType.AddKeys(airlineCodeProp); airlineType.AddProperty(airlineCodeProp); airlineType.AddProperty(new EdmStructuralProperty(airlineType, "Name", EdmCoreModel.Instance.GetString(false))); model.AddElement(airlineType); var airlineSet = new EdmEntitySet(defaultContainer, "Airlines", airlineType); defaultContainer.AddElement(airlineSet); var airportType = new EdmEntityType(ns, "Airport"); var airportIdType = new EdmStructuralProperty(airportType, "IcaoCode", EdmCoreModel.Instance.GetString(false)); airportType.AddProperty(airportIdType); airportType.AddKeys(airportIdType); airportType.AddProperty(new EdmStructuralProperty(airportType, "Name", EdmCoreModel.Instance.GetString(false))); airportType.AddProperty(new EdmStructuralProperty(airportType, "IataCode", EdmCoreModel.Instance.GetString(false))); airportType.AddProperty(new EdmStructuralProperty(airportType, "Location", new EdmComplexTypeReference(airportLocationType, false))); model.AddElement(airportType); var airportSet = new EdmEntitySet(defaultContainer, "Airports", airportType); defaultContainer.AddElement(airportSet); var planItemType = new EdmEntityType(ns, "PlanItem"); var planItemIdType = new EdmStructuralProperty(planItemType, "PlanItemId", EdmCoreModel.Instance.GetInt32(false)); planItemType.AddProperty(planItemIdType); planItemType.AddKeys(planItemIdType); planItemType.AddProperty(new EdmStructuralProperty(planItemType, "ConfirmationCode", EdmCoreModel.Instance.GetString(true))); planItemType.AddProperty(new EdmStructuralProperty(planItemType, "StartsAt", EdmCoreModel.Instance.GetDateTimeOffset(true))); planItemType.AddProperty(new EdmStructuralProperty(planItemType, "EndsAt", EdmCoreModel.Instance.GetDateTimeOffset(true))); planItemType.AddProperty(new EdmStructuralProperty(planItemType, "Duration", EdmCoreModel.Instance.GetDuration(true))); model.AddElement(planItemType); var publicTransportationType = new EdmEntityType(ns, "PublicTransportation", planItemType); publicTransportationType.AddProperty(new EdmStructuralProperty(publicTransportationType, "SeatNumber", EdmCoreModel.Instance.GetString(true))); model.AddElement(publicTransportationType); var flightType = new EdmEntityType(ns, "Flight", publicTransportationType); var flightNumberType = new EdmStructuralProperty(flightType, "FlightNumber", EdmCoreModel.Instance.GetString(false)); flightType.AddProperty(flightNumberType); model.AddElement(flightType); var eventType = new EdmEntityType(ns, "Event", planItemType, false, true); eventType.AddProperty(new EdmStructuralProperty(eventType, "Description", EdmCoreModel.Instance.GetString(true))); eventType.AddProperty(new EdmStructuralProperty(eventType, "OccursAt", new EdmComplexTypeReference(eventLocationType, false))); model.AddElement(eventType); var tripType = new EdmEntityType(ns, "Trip"); var tripIdType = new EdmStructuralProperty(tripType, "TripId", EdmCoreModel.Instance.GetInt32(false)); tripType.AddProperty(tripIdType); tripType.AddKeys(tripIdType); tripType.AddProperty(new EdmStructuralProperty(tripType, "ShareId", EdmCoreModel.Instance.GetGuid(true))); tripType.AddProperty(new EdmStructuralProperty(tripType, "Description", EdmCoreModel.Instance.GetString(true))); tripType.AddProperty(new EdmStructuralProperty(tripType, "Name", EdmCoreModel.Instance.GetString(false))); tripType.AddProperty(new EdmStructuralProperty(tripType, "Budget", EdmCoreModel.Instance.GetSingle(false))); tripType.AddProperty(new EdmStructuralProperty(tripType, "StartsAt", EdmCoreModel.Instance.GetDateTimeOffset(false))); tripType.AddProperty(new EdmStructuralProperty(tripType, "EndsAt", EdmCoreModel.Instance.GetDateTimeOffset(false))); tripType.AddProperty(new EdmStructuralProperty(tripType, "Tags", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(false))))); model.AddElement(tripType); var friendsnNavigation = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Friends", Target = personType, TargetMultiplicity = EdmMultiplicity.Many }); var personTripNavigation = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Trips", Target = tripType, TargetMultiplicity = EdmMultiplicity.Many, ContainsTarget = true }); var personPhotoNavigation = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Photo", Target = photoType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne, }); var tripPhotosNavigation = tripType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Photos", Target = photoType, TargetMultiplicity = EdmMultiplicity.Many, }); var tripItemNavigation = tripType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "PlanItems", Target = planItemType, ContainsTarget = true, TargetMultiplicity = EdmMultiplicity.Many }); var flightFromAirportNavigation = flightType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "From", Target = airportType, TargetMultiplicity = EdmMultiplicity.One }); var flightToAirportNavigation = flightType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "To", Target = airportType, TargetMultiplicity = EdmMultiplicity.One }); var flightAirlineNavigation = flightType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Airline", Target = airlineType, TargetMultiplicity = EdmMultiplicity.One }); var me = new EdmSingleton(defaultContainer, "Me", personType); defaultContainer.AddElement(me); personSet.AddNavigationTarget(friendsnNavigation, personSet); me.AddNavigationTarget(friendsnNavigation, personSet); personSet.AddNavigationTarget(flightAirlineNavigation, airlineSet); me.AddNavigationTarget(flightAirlineNavigation, airlineSet); personSet.AddNavigationTarget(flightFromAirportNavigation, airportSet); me.AddNavigationTarget(flightFromAirportNavigation, airportSet); personSet.AddNavigationTarget(flightToAirportNavigation, airportSet); me.AddNavigationTarget(flightToAirportNavigation, airportSet); personSet.AddNavigationTarget(personPhotoNavigation, photoSet); me.AddNavigationTarget(personPhotoNavigation, photoSet); personSet.AddNavigationTarget(tripPhotosNavigation, photoSet); me.AddNavigationTarget(tripPhotosNavigation, photoSet); var getFavoriteAirlineFunction = new EdmFunction(ns, "GetFavoriteAirline", new EdmEntityTypeReference(airlineType, false), true, new EdmPathExpression("person/Trips/PlanItems/Microsoft.OData.SampleService.Models.TripPin.Flight/Airline"), true); getFavoriteAirlineFunction.AddParameter("person", new EdmEntityTypeReference(personType, false)); model.AddElement(getFavoriteAirlineFunction); var getInvolvedPeopleFunction = new EdmFunction(ns, "GetInvolvedPeople", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(personType, false))), true, null, true); getInvolvedPeopleFunction.AddParameter("trip", new EdmEntityTypeReference(tripType, false)); model.AddElement(getInvolvedPeopleFunction); var getFriendsTripsFunction = new EdmFunction(ns, "GetFriendsTrips", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(tripType, false))), true, new EdmPathExpression("person/Friends/Trips"), true); getFriendsTripsFunction.AddParameter("person", new EdmEntityTypeReference(personType, false)); getFriendsTripsFunction.AddParameter("userName", EdmCoreModel.Instance.GetString(false)); model.AddElement(getFriendsTripsFunction); var getNearestAirport = new EdmFunction(ns, "GetNearestAirport", new EdmEntityTypeReference(airportType, false), false, null, true); getNearestAirport.AddParameter("lat", EdmCoreModel.Instance.GetDouble(false)); getNearestAirport.AddParameter("lon", EdmCoreModel.Instance.GetDouble(false)); model.AddElement(getNearestAirport); var getNearestAirportFunctionImport = (IEdmFunctionImport)defaultContainer.AddFunctionImport("GetNearestAirport", getNearestAirport, new EdmEntitySetReferenceExpression(airportSet), true); var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null); model.AddElement(resetDataSourceAction); defaultContainer.AddActionImport(resetDataSourceAction); var shareTripAction = new EdmAction(ns, "ShareTrip", null, true, null); shareTripAction.AddParameter("person", new EdmEntityTypeReference(personType, false)); shareTripAction.AddParameter("userName", EdmCoreModel.Instance.GetString(false)); shareTripAction.AddParameter("tripId", EdmCoreModel.Instance.GetInt32(false)); model.AddElement(shareTripAction); model.SetDescriptionAnnotation(defaultContainer, "TripPin service is a sample service for OData V4."); model.SetOptimisticConcurrencyAnnotation(personSet, personType.StructuralProperties().Where(p => p.Name == "Concurrency")); // TODO: currently singleton does not support ETag feature // model.SetOptimisticConcurrencyAnnotation(me, personType.StructuralProperties().Where(p => p.Name == "Concurrency")); model.SetResourcePathCoreAnnotation(personSet, "People"); model.SetResourcePathCoreAnnotation(me, "Me"); model.SetResourcePathCoreAnnotation(airlineSet, "Airlines"); model.SetResourcePathCoreAnnotation(airportSet, "Airports"); model.SetResourcePathCoreAnnotation(photoSet, "Photos"); model.SetResourcePathCoreAnnotation(getNearestAirportFunctionImport, "Microsoft.OData.SampleService.Models.TripPin.GetNearestAirport"); model.SetDereferenceableIDsCoreAnnotation(defaultContainer, true); model.SetConventionalIDsCoreAnnotation(defaultContainer, true); model.SetPermissionsCoreAnnotation(personType.FindProperty("UserName"), CorePermission.Read); model.SetPermissionsCoreAnnotation(airlineType.FindProperty("AirlineCode"), CorePermission.Read); model.SetPermissionsCoreAnnotation(airportType.FindProperty("IcaoCode"), CorePermission.Read); model.SetPermissionsCoreAnnotation(planItemType.FindProperty("PlanItemId"), CorePermission.Read); model.SetPermissionsCoreAnnotation(tripType.FindProperty("TripId"), CorePermission.Read); model.SetPermissionsCoreAnnotation(photoType.FindProperty("Id"), CorePermission.Read); model.SetImmutableCoreAnnotation(airportType.FindProperty("IataCode"), true); model.SetComputedCoreAnnotation(personType.FindProperty("Concurrency"), true); model.SetAcceptableMediaTypesCoreAnnotation(photoType, new[] { "image/jpeg" }); model.SetConformanceLevelCapabilitiesAnnotation(defaultContainer, CapabilitiesConformanceLevelType.Advanced); model.SetSupportedFormatsCapabilitiesAnnotation(defaultContainer, new[] { "application/json;odata.metadata=full;IEEE754Compatible=false;odata.streaming=true", "application/json;odata.metadata=minimal;IEEE754Compatible=false;odata.streaming=true", "application/json;odata.metadata=none;IEEE754Compatible=false;odata.streaming=true" }); model.SetAsynchronousRequestsSupportedCapabilitiesAnnotation(defaultContainer, true); model.SetBatchContinueOnErrorSupportedCapabilitiesAnnotation(defaultContainer, false); model.SetNavigationRestrictionsCapabilitiesAnnotation(personSet, CapabilitiesNavigationType.None, new[] { new Tuple <IEdmNavigationProperty, CapabilitiesNavigationType>(friendsnNavigation, CapabilitiesNavigationType.Recursive) }); model.SetFilterFunctionsCapabilitiesAnnotation(defaultContainer, new[] { "contains", "endswith", "startswith", "length", "indexof", "substring", "tolower", "toupper", "trim", "concat", "year", "month", "day", "hour", "minute", "second", "round", "floor", "ceiling", "cast", "isof" }); model.SetSearchRestrictionsCapabilitiesAnnotation(personSet, true, CapabilitiesSearchExpressions.None); model.SetSearchRestrictionsCapabilitiesAnnotation(airlineSet, true, CapabilitiesSearchExpressions.None); model.SetSearchRestrictionsCapabilitiesAnnotation(airportSet, true, CapabilitiesSearchExpressions.None); model.SetSearchRestrictionsCapabilitiesAnnotation(photoSet, true, CapabilitiesSearchExpressions.None); model.SetInsertRestrictionsCapabilitiesAnnotation(personSet, true, new[] { personTripNavigation, friendsnNavigation }); model.SetInsertRestrictionsCapabilitiesAnnotation(airlineSet, true, null); model.SetInsertRestrictionsCapabilitiesAnnotation(airportSet, false, null); model.SetInsertRestrictionsCapabilitiesAnnotation(photoSet, true, null); // TODO: model.SetUpdateRestrictionsCapabilitiesAnnotation(); model.SetDeleteRestrictionsCapabilitiesAnnotation(airportSet, false, null); model.SetISOCurrencyMeasuresAnnotation(tripType.FindProperty("Budget"), "USD"); model.SetScaleMeasuresAnnotation(tripType.FindProperty("Budget"), 2); return(model); }
private static IEdmModel CreateEdmModel() { var model = new EdmModel(); var enumType = new EdmEnumType("DefaultNs", "Color"); var blue = enumType.AddMember("Blue", new EdmEnumMemberValue(0)); enumType.AddMember("White", new EdmEnumMemberValue(1)); model.AddElement(enumType); 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); return(model); }
internal static IEdmModel GetEdmModel() { var model = new EdmModel(); // open address var address = new EdmComplexType("NS", "Address", null, false, true); address.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(true)); address.AddStructuralProperty("City", EdmCoreModel.Instance.GetString(true)); model.AddElement(address); // customer var customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false))); customer.AddStructuralProperty("HomeAddress", new EdmComplexTypeReference(address, true)); model.AddElement(customer); // order var order = new EdmEntityType("NS", "Order"); order.AddKeys(order.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false))); model.AddElement(order); var nav = customer.AddBidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Orders", Target = order, TargetMultiplicity = EdmMultiplicity.Many }, new EdmNavigationPropertyInfo { Name = "Customer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, }); var container = new EdmEntityContainer("Default", "Container"); model.AddElement(container); var customers = new EdmEntitySet(container, "Customers", customer); container.AddElement(customers); var orders = new EdmEntitySet(container, "Orders", order); container.AddElement(orders); customers.AddNavigationTarget(nav, orders); // bound operations var customerReference = new EdmEntityTypeReference(customer, true); var orderReference = new EdmEntityTypeReference(order, true); IEdmPathExpression path = new EdmPathExpression("binding/Orders"); // entityset path & return type var function = new EdmFunction("NS", "GetSomeOrders", new EdmCollectionTypeReference(new EdmCollectionType(orderReference)), true, path, true /*isComposable*/); function.AddParameter("binding", customerReference); model.AddElement(function); function = new EdmFunction("NS", "GetAnOrder", orderReference, true, path, true /*isComposable*/); function.AddParameter("binding", customerReference); model.AddElement(function); // GetSomeAddressFromCustomer function = new EdmFunction("NS", "GetSomeAddressFromCustomer1", new EdmComplexTypeReference(address, true), true, null, true /*isComposable*/); function.AddParameter("binding", new EdmEntityTypeReference(customer, true)); model.AddElement(function); // We leave the "GetSomeAddressFromCustomer2" code here. // However, the operation with the entity set path containing a complex type doesn't make sense. IEdmPathExpression complexPath = new EdmPathExpression("binding/HomeAddress"); function = new EdmFunction("NS", "GetSomeAddressFromCustomer2", new EdmComplexTypeReference(address, true), true, complexPath, true /*isComposable*/); function.AddParameter("binding", new EdmEntityTypeReference(customer, true)); model.AddElement(function); function = new EdmFunction("NS", "GetSomeAddressFromCustomer3", new EdmComplexTypeReference(address, true), true, null, true /*isComposable*/); function.AddParameter("binding", new EdmEntityTypeReference(customer, true)); model.AddElement(function); var action = new EdmAction("NS", "DoSomeThing", null, true, null); action.AddParameter("binding", new EdmEntityTypeReference(customer, true)); model.AddElement(action); // operation import function = new EdmFunction("NS", "GetSomeOrders2", new EdmCollectionTypeReference(new EdmCollectionType(orderReference))); EdmFunctionImport functionImport = new EdmFunctionImport(container, "GetSomeOrders2", function, new EdmPathExpression("Orders"), false); container.AddElement(functionImport); function = new EdmFunction("NS", "GetAnOrder2", orderReference); functionImport = new EdmFunctionImport(container, "GetAnOrder2", function, new EdmPathExpression("Orders"), false); container.AddElement(functionImport); function = new EdmFunction("NS", "GetSomeAddress", new EdmComplexTypeReference(address, true), false, null, true); functionImport = new EdmFunctionImport(container, "GetSomeAddress", function, null, false); container.AddElement(functionImport); action = new EdmAction("NS", "DoSomeThing2", null, false, null); var actionImport = new EdmActionImport(container, "DoSomeThing2", action, null); container.AddElement(actionImport); return(model); }
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); }
private static EdmModel GetModel() { var model = new EdmModel(); var person = new EdmEntityType("DefaultNs", "Person"); var entityId = person.AddStructuralProperty("UserName", EdmCoreModel.Instance.GetString(false)); person.AddKeys(entityId); var employee = new EdmEntityType("DefaultNs", "Employee", person); var city = new EdmEntityType("DefaultNs", "City"); var cityId = city.AddStructuralProperty("ZipCode", EdmCoreModel.Instance.GetInt32(false)); city.AddKeys(cityId); var personCity = new EdmEntityType("DefaultNs", "PersonCity"); var personcityId = personCity.AddStructuralProperty("ZipCode", EdmCoreModel.Instance.GetInt32(false)); personCity.AddKeys(personcityId); var city3 = new EdmEntityType("DefaultNs", "City3"); var cityId3 = city.AddStructuralProperty("ZipCode3", EdmCoreModel.Instance.GetInt32(false)); city.AddKeys(cityId3); var region = new EdmEntityType("DefaultNs", "Region"); var regionId = region.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); region.AddKeys(regionId); var cityRegion = city.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Region", Target = region, TargetMultiplicity = EdmMultiplicity.One, }); var complex = new EdmComplexType("DefaultNs", "Address"); complex.AddStructuralProperty("Road", EdmCoreModel.Instance.GetString(false)); var subcomplex = new EdmComplexType("DefaultNs", "SubAddress"); subcomplex.AddStructuralProperty("SubRoad", EdmCoreModel.Instance.GetString(false)); complex.AddStructuralProperty("SubAddress", new EdmComplexTypeReference(subcomplex, false)); var navP = complex.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo() { Name = "City", Target = city, TargetMultiplicity = EdmMultiplicity.One, }); var navP3 = complex.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo() { Name = "City3", Target = city3, TargetMultiplicity = EdmMultiplicity.One, }); var derivedComplex = new EdmComplexType("DefaultNs", "WorkAddress", complex); var navP2 = derivedComplex.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo() { Name = "City2", Target = city, TargetMultiplicity = EdmMultiplicity.One, }); complex.AddStructuralProperty("WorkAddress", new EdmComplexTypeReference(complex, false)); person.AddStructuralProperty("Address", new EdmComplexTypeReference(complex, false)); person.AddStructuralProperty("Addresses", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(complex, false)))); var navP4 = person.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo() { Name = "PersonCity", Target = personCity, TargetMultiplicity = EdmMultiplicity.One, }); model.AddElement(person); model.AddElement(employee); model.AddElement(city); model.AddElement(personCity); model.AddElement(city3); model.AddElement(region); 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 pcities = new EdmEntitySet(entityContainer, "PersonCity", personCity); EdmEntitySet regions = new EdmEntitySet(entityContainer, "Regions", region); people.AddNavigationTarget(navP, cities, new EdmPathExpression("Address/City")); people.AddNavigationTarget(navP3, cities, new EdmPathExpression("Address/City3")); people.AddNavigationTarget(navP, cities, new EdmPathExpression("Addresses/City")); people.AddNavigationTarget(navP2, cities, new EdmPathExpression("Address/WorkAddress/DefaultNs.WorkAddress/City2")); people.AddNavigationTarget(navP4, pcities, new EdmPathExpression("PersonCity")); cities.AddNavigationTarget(cityRegion, regions); entityContainer.AddElement(people); entityContainer.AddElement(cities); entityContainer.AddElement(pcities); return(model); }
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); }
public EdmModel BuildEdmModel() { Dictionary <Type, EntityTypeInfo> entityTypeInfos = BuildEntityTypes(); foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { typeInfo.BuildProperties(entityTypeInfos, _enumTypes, _complexTypes); } foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties) { fkeyInfo.EdmNavigationProperty = CreateNavigationProperty(fkeyInfo); } } var edmModel = new EdmModel(); var container = new EdmEntityContainer("Default", "Container"); edmModel.AddElements(_enumTypes.Values); foreach (KeyValuePair <Type, EdmEnumType> enumType in _enumTypes) { edmModel.SetClrType(enumType.Value, enumType.Key); } edmModel.AddElements(_complexTypes.Values); foreach (KeyValuePair <Type, EdmComplexType> complexType in _complexTypes) { edmModel.SetClrType(complexType.Value, complexType.Key); } var entitySets = new Dictionary <IEdmEntityType, EdmEntitySet>(entityTypeInfos.Count); foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { edmModel.AddElement(typeInfo.EdmType); edmModel.SetClrType(typeInfo.EdmType, typeInfo.ClrType); foreach (KeyValuePair <String, Type> pair in _entitySets) { if (pair.Value == typeInfo.ClrType) { entitySets.Add(typeInfo.EdmType, container.AddEntitySet(pair.Key, typeInfo.EdmType)); break; } } } foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values) { foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties) { EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType]; EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType]; dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal); if (fkeyInfo.EdmNavigationProperty.Partner != null) { principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent); } } } foreach (OeOperationConfiguration operationConfiguration in _operationConfigurations) { if (operationConfiguration.IsEdmFunction) { EdmFunction edmFunction = BuildFunction(operationConfiguration, entityTypeInfos); if (edmFunction != null) { edmModel.AddElement(edmFunction); container.AddFunctionImport(operationConfiguration.Name, edmFunction); edmModel.SetIsDbFunction(edmFunction, operationConfiguration.IsDbFunction); } } else { EdmAction edmAction = BuildAction(operationConfiguration, entityTypeInfos); if (edmAction != null) { edmModel.AddElement(edmAction); container.AddActionImport(operationConfiguration.Name, edmAction); edmModel.SetIsDbFunction(edmAction, operationConfiguration.IsDbFunction); } } } edmModel.AddElement(container); return(edmModel); }
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; }
/// <summary> /// Creates a set of interesting entity instances along with metadata. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <param name="model">If non-null, the method creates complex types for the complex values and adds them to the model.</param> /// <param name="withTypeNames">true if the payloads should specify type names.</param> /// <returns>List of test descriptors with interesting entity instances as payload.</returns> public static IEnumerable <PayloadTestDescriptor> CreateEntityInstanceTestDescriptors( EdmModel model, bool withTypeNames) { IEnumerable <PrimitiveValue> primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(fullSet: false); IEnumerable <ComplexInstance> complexValues = TestValues.CreateComplexValues(model, withTypeNames, fullSet: false); IEnumerable <NamedStreamInstance> streamReferenceValues = TestValues.CreateStreamReferenceValues(fullSet: false); IEnumerable <PrimitiveMultiValue> primitiveMultiValues = TestValues.CreatePrimitiveCollections(withTypeNames, fullSet: false); IEnumerable <ComplexMultiValue> complexMultiValues = TestValues.CreateComplexCollections(model, withTypeNames, fullSet: false); IEnumerable <NavigationPropertyInstance> navigationProperties = TestValues.CreateDeferredNavigationLinks(); // NOTE we have to copy the EntityModelTypeAnnotation on the primitive value to the NullPropertyInstance for null values since the // NullPropertyInstance does not expose a value. We will later copy it back to the value we generate for the null property. IEnumerable <PropertyInstance> primitiveProperties = primitiveValues.Select((pv, ix) => PayloadBuilder.Property("PrimitiveProperty" + ix, pv).CopyAnnotation <PropertyInstance, EntityModelTypeAnnotation>(pv)); IEnumerable <PropertyInstance> complexProperties = complexValues.Select((cv, ix) => PayloadBuilder.Property("ComplexProperty" + ix, cv)); IEnumerable <PropertyInstance> primitiveMultiValueProperties = primitiveMultiValues.Select((pmv, ix) => PayloadBuilder.Property("PrimitiveMultiValueProperty" + ix, pmv)); IEnumerable <PropertyInstance> complexMultiValueProperties = complexMultiValues.Select((cmv, ix) => PayloadBuilder.Property("ComplexMultiValueProperty" + ix, cmv)); PropertyInstance[][] propertyMatrix = new PropertyInstance[6][]; propertyMatrix[0] = primitiveProperties.ToArray(); propertyMatrix[1] = complexProperties.ToArray(); propertyMatrix[2] = streamReferenceValues.ToArray(); propertyMatrix[3] = primitiveMultiValueProperties.ToArray(); propertyMatrix[4] = complexMultiValueProperties.ToArray(); propertyMatrix[5] = navigationProperties.ToArray(); IEnumerable <PropertyInstance[]> propertyCombinations = propertyMatrix.ColumnCombinations(0, 1, 6); int count = 0; foreach (PropertyInstance[] propertyCombination in propertyCombinations) { // build the entity type, add it to the model EdmEntityType generatedEntityType = null; string typeName = "PGEntityType" + count; EdmEntityContainer container = null; EdmEntitySet entitySet = null; if (model != null) { // generate a new type with the auto-generated name, check that no type with this name exists and add the default key property to it. Debug.Assert(model.FindDeclaredType(typeName) == null, "Entity type '" + typeName + "' already exists."); generatedEntityType = new EdmEntityType("TestModel", typeName); generatedEntityType.AddKeys(generatedEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); model.AddElement(generatedEntityType); container = model.EntityContainer as EdmEntityContainer; if (container == null) { container = new EdmEntityContainer("TestModel", "DefaultNamespace"); model.AddElement(container); } entitySet = container.AddEntitySet(typeName, generatedEntityType); } EntityInstance entityInstance = PayloadBuilder.Entity("TestModel." + typeName) .Property("Id", PayloadBuilder.PrimitiveValue(count).WithTypeAnnotation(EdmCoreModel.Instance.GetInt32(false))); for (int i = 0; i < propertyCombination.Length; ++i) { PropertyInstance currentProperty = propertyCombination[i]; entityInstance.Add(currentProperty); if (model != null) { if (entitySet == null) { entitySet = container.FindEntitySet(typeName) as EdmEntitySet; } switch (currentProperty.ElementType) { case ODataPayloadElementType.ComplexProperty: ComplexProperty complexProperty = (ComplexProperty)currentProperty; generatedEntityType.AddStructuralProperty(complexProperty.Name, complexProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); break; case ODataPayloadElementType.PrimitiveProperty: PrimitiveProperty primitiveProperty = (PrimitiveProperty)currentProperty; if (primitiveProperty.Value == null) { generatedEntityType.AddStructuralProperty( primitiveProperty.Name, PayloadBuilder.PrimitiveValueType(null)); } else { generatedEntityType.AddStructuralProperty(primitiveProperty.Name, primitiveProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); } break; case ODataPayloadElementType.NamedStreamInstance: NamedStreamInstance streamProperty = (NamedStreamInstance)currentProperty; generatedEntityType.AddStructuralProperty(streamProperty.Name, EdmPrimitiveTypeKind.Stream); break; case ODataPayloadElementType.EmptyCollectionProperty: throw new NotImplementedException(); case ODataPayloadElementType.NavigationPropertyInstance: NavigationPropertyInstance navigationProperty = (NavigationPropertyInstance)currentProperty; var navProperty = generatedEntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { ContainsTarget = false, Name = navigationProperty.Name, Target = generatedEntityType, TargetMultiplicity = EdmMultiplicity.One }); entitySet.AddNavigationTarget(navProperty, entitySet); break; case ODataPayloadElementType.ComplexMultiValueProperty: ComplexMultiValueProperty complexMultiValueProperty = (ComplexMultiValueProperty)currentProperty; generatedEntityType.AddStructuralProperty(complexMultiValueProperty.Name, complexMultiValueProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); break; case ODataPayloadElementType.PrimitiveMultiValueProperty: PrimitiveMultiValueProperty primitiveMultiValueProperty = (PrimitiveMultiValueProperty)currentProperty; generatedEntityType.AddStructuralProperty(primitiveMultiValueProperty.Name, primitiveMultiValueProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); break; default: throw new NotSupportedException("Unsupported element type found : " + propertyCombination[i].ElementType); } } } if (generatedEntityType != null) { entityInstance.AddAnnotation(new EntityModelTypeAnnotation(generatedEntityType.ToTypeReference(true))); } yield return(new PayloadTestDescriptor() { PayloadElement = entityInstance, PayloadEdmModel = model }); count++; } }