private static EdmModel CreateCustomerProductsModel()
        {
            var model = new EdmModel();

            var container = new EdmEntityContainer("defaultNamespace", "DefaultContainer");
            model.AddElement(container);

            var productType = new EdmEntityType("defaultNamespace", "Product");
            model.AddElement(productType);

            var customerType = new EdmEntityType("defaultNamespace", "Customer");
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Products", Target = productType, TargetMultiplicity = EdmMultiplicity.Many });
            productType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Customers", Target = customerType, TargetMultiplicity = EdmMultiplicity.Many });

            model.AddElement(customerType);

            var productSet = new EdmEntitySet(
                container,
                "Products",
                productType);

            container.AddElement(productSet);

            var customerSet = new EdmEntitySet(
                container,
                "Customers",
                customerType);

            var productsNavProp = customerType.NavigationProperties().Single(np => np.Name == "Products");
            customerSet.AddNavigationTarget(productsNavProp, productSet);
            container.AddElement(customerSet);

            return model;
        }
        public static IEdmModel BuildEdmModel(ODataModelBuilder builder)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }

            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName);

            // add types and sets, building an index on the way.
            Dictionary<Type, IEdmStructuredType> edmTypeMap = model.AddTypes(builder.StructuralTypes);
            Dictionary<string, EdmEntitySet> edmEntitySetMap = model.AddEntitySets(builder, container, edmTypeMap);

            // add procedures
            model.AddProcedures(builder.Procedures, container, edmTypeMap, edmEntitySetMap);

            // finish up
            model.AddElement(container);

            // build the map from IEdmEntityType to IEdmFunctionImport
            model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model));

            return model;
        }
        private static Dictionary<string, EdmEntitySet> AddEntitySets(this EdmModel model, ODataModelBuilder builder, EdmEntityContainer container, Dictionary<Type, IEdmStructuredType> edmTypeMap)
        {
            IEnumerable<EntitySetConfiguration> configurations = builder.EntitySets;

            // build the entitysets and their annotations
            IEnumerable<Tuple<EdmEntitySet, EntitySetConfiguration>> entitySets = AddEntitySets(configurations, container, edmTypeMap);
            var entitySetAndAnnotations = entitySets.Select(e => new
            {
                EntitySet = e.Item1,
                Configuration = e.Item2,
                Annotations = new
                {
                    LinkBuilder = new EntitySetLinkBuilderAnnotation(e.Item2),
                    Url = new EntitySetUrlAnnotation { Url = e.Item2.GetUrl() }
                }
            }).ToArray();

            // index the entitySets by name
            Dictionary<string, EdmEntitySet> edmEntitySetMap = entitySetAndAnnotations.ToDictionary(e => e.EntitySet.Name, e => e.EntitySet);

            // apply the annotations
            foreach (var iter in entitySetAndAnnotations)
            {
                EdmEntitySet entitySet = iter.EntitySet;
                model.SetAnnotationValue<EntitySetUrlAnnotation>(entitySet, iter.Annotations.Url);
                model.SetAnnotationValue<IEntitySetLinkBuilder>(entitySet, iter.Annotations.LinkBuilder);

                AddNavigationBindings(iter.Configuration, iter.EntitySet, iter.Annotations.LinkBuilder, builder, edmTypeMap, edmEntitySetMap);
            }
            return edmEntitySetMap;
        }
        public void GetEntitySetLinkBuilder_ReturnsDefaultEntitySetBuilder_IfNotSet()
        {
            IEdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            EdmEntityType entityType = new EdmEntityType("NS", "Entity");
            IEdmEntitySet entitySet = new EdmEntitySet(container, "EntitySet", entityType);

            Assert.NotNull(model.GetEntitySetLinkBuilder(entitySet));
        }
        public void GetActionLinkBuilder_ReturnsDefaultActionLinkBuilder_IfNotSet()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            IEdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            IEdmFunctionImport action = new EdmFunctionImport(container, "Action", returnType: null);

            Assert.NotNull(model.GetActionLinkBuilder(action));
        }
        public static IEdmModel SimpleCustomerOrderModel()
        {
            var model = new EdmModel();
            var customerType = new EdmEntityType("Default", "Customer");
            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance
                .GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: true);
            customerType.AddStructuralProperty("City", primitiveTypeReference, defaultValue: null,
                concurrencyMode: EdmConcurrencyMode.Fixed);
            model.AddElement(customerType);

            var orderType = new EdmEntityType("Default", "Order");
            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            orderType.AddStructuralProperty("Shipment", EdmPrimitiveTypeKind.String);
            model.AddElement(orderType);

            var addressType = new EdmComplexType("Default", "Address");
            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // Add navigations
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Orders",
                Target = orderType,
                TargetMultiplicity = EdmMultiplicity.Many
            });
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Customer",
                Target = customerType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var container = new EdmEntityContainer("Default", "Container");
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet = container.AddEntitySet("Orders", orderType);
            customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet);
            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet);

            EntitySetLinkBuilderAnnotation linkAnnotation = new MockEntitySetLinkBuilderAnnotation();
            model.SetEntitySetLinkBuilder(customerSet, linkAnnotation);
            model.SetEntitySetLinkBuilder(orderSet, linkAnnotation);

            model.AddElement(container);
            return model;
        }
        public IEdmModel ToEdmModel(JSchema schema)
        {
            var model = new EdmModel();

            var container = new EdmEntityContainer("namespace", "containerName");

            var rootType = ConvertToEdmEntityType(schema, model, "root");

            container.AddEntitySet("root", rootType);
            model.AddElement(container);

            return model;
        }
        static TableStorageQueryIndexInspector()
        {
            EdmModel = new EdmModel();
            TableServiceEntity = new EdmEntityType("Glimpse.WindowsAzure.Storage", "TableServiceEntity");
            TableServiceEntity.AddStructuralProperty("PartitionKey", EdmPrimitiveTypeKind.String);
            TableServiceEntity.AddStructuralProperty("RowKey", EdmPrimitiveTypeKind.String);
            TableServiceEntity.AddStructuralProperty("Timestamp", EdmPrimitiveTypeKind.DateTime);
            EdmModel.AddElement(TableServiceEntity);

            var container = new EdmEntityContainer("TestModel", "DefaultContainer");
            container.AddEntitySet("Entities", TableServiceEntity);
            EdmModel.AddElement(container);
        }
        public void GetActionLinkBuilder_After_SetActionLinkBuilder()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            IEdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            IEdmFunctionImport action = new Mock<IEdmFunctionImport>().Object;
            ActionLinkBuilder builder = new ActionLinkBuilder(_=> null, followsConventions: false);

            // Act
            model.SetActionLinkBuilder(action, builder);
            var result = model.GetActionLinkBuilder(action);

            // Assert
            Assert.Same(builder, result);
        }
        public void GetEntitySetLinkBuilder_After_SetEntitySetLinkBuilder()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            EdmEntityType entityType = new EdmEntityType("NS", "Entity");
            IEdmEntitySet entitySet = new EdmEntitySet(container, "EntitySet", entityType);
            EntitySetLinkBuilderAnnotation entitySetLinkBuilder = new EntitySetLinkBuilderAnnotation();

            // Act
            model.SetEntitySetLinkBuilder(entitySet, entitySetLinkBuilder);
            var result = model.GetEntitySetLinkBuilder(entitySet);

            // Assert
            Assert.Same(entitySetLinkBuilder, result);
        }
        public IEdmModel BuildModel(IEnumerable<TableData> tables)
        {
            var model = new EdmModel();            
            var container = new EdmEntityContainer("sitecore.com", "Visits");
            model.AddElement(container);            
            

            var tableMap = new Dictionary<TableDataSchema, EdmEntityTypeWrapper>();
            foreach (var table in tables)
            {
                var typeWrapper = AddTable(model, table);
                container.AddEntitySet(table.Name, typeWrapper.Type);
                tableMap.Add(table.Schema, typeWrapper);
            }

            foreach (var t in tableMap)
            {
                foreach (var reference in t.Key.RelatedTables)
                {
                    if (reference.RelationType == RelationType.Child || reference.RelationType == RelationType.DimensionReference)
                    {
                        var source = t.Value;
                        var target = tableMap[reference.RelatedTable];
                                                

                        t.Value.Type.AddBidirectionalNavigation(new EdmNavigationPropertyInfo()
                        {
                            Name = reference.RelatedTable.Name,
                            TargetMultiplicity = EdmMultiplicity.Many,                            
                            Target = target.Type
                        }, new EdmNavigationPropertyInfo()
                        {
                            Name = t.Key.Name,
                            TargetMultiplicity = reference.RelationType == RelationType.Child ? EdmMultiplicity.One : EdmMultiplicity.ZeroOrOne,
                            DependentProperties = reference.RelatedFields.Select(f=>target.Properties[f]),
                            Target = source.Type
                        });
                    }
                }
            }

            return model;
        }
        public void EntityContainer_Is_Default_ShowsUp_In_Metadata()
        {
            // Arrange
            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("Default", "SampleContainer");
            model.AddElement(container);

            // Act
            model.SetIsDefaultEntityContainer(container, isDefaultContainer: true);

            // Assert
            MemoryStream stream = new MemoryStream();
            ODataMessageWriter writer = new ODataMessageWriter(new ODataMessageWrapper(stream) as IODataResponseMessage, new ODataMessageWriterSettings(), model);
            writer.WriteMetadataDocument();
            stream.Seek(0, SeekOrigin.Begin);
            XElement element = XElement.Load(stream);
            var containerXml = element.Descendants().Where(n => n.Name.LocalName == "EntityContainer").SingleOrDefault();
            Assert.Equal("SampleContainer", containerXml.Attribute("Name").Value);
            Assert.Equal("true", containerXml.Attributes().Where(a => a.Name.LocalName == "IsDefaultEntityContainer").Single().Value);
        }
        private static Dictionary<string, EdmEntitySet> AddEntitySets(this EdmModel model, IEnumerable<IEntitySetConfiguration> configurations, EdmEntityContainer container, Dictionary<string, IEdmStructuredType> edmTypeMap)
        {
            // build the entitysets and their annotations
            IEnumerable<Tuple<EdmEntitySet, IEntitySetConfiguration>> entitySets = AddEntitySets(configurations, container, edmTypeMap);
            var entitySetAndAnnotations = entitySets.Select(e => new
            {
                EntitySet = e.Item1,
                Configuration = e.Item2,
                Annotations = new
                {
                    LinkBuilder = new EntitySetLinkBuilderAnnotation(e.Item2),
                    Url = new EntitySetUrlAnnotation { Url = e.Item2.GetUrl() }
                }
            }).ToArray();

            // index the entitySets by name
            Dictionary<string, EdmEntitySet> edmEntitySetMap = entitySetAndAnnotations.ToDictionary(e => e.EntitySet.Name, e => e.EntitySet);

            // apply the annotations
            foreach (var iter in entitySetAndAnnotations)
            {
                EdmEntitySet entitySet = iter.EntitySet;
                IEntitySetConfiguration configuration = iter.Configuration;
                model.SetAnnotationValue<EntitySetUrlAnnotation>(entitySet, iter.Annotations.Url);
                model.SetAnnotationValue<IEntitySetLinkBuilder>(entitySet, iter.Annotations.LinkBuilder);

                foreach (NavigationPropertyConfiguration navigation in configuration.EntityType.NavigationProperties)
                {
                    NavigationPropertyBinding binding = configuration.FindBinding(navigation);
                    if (binding != null)
                    {
                        EdmEntityType edmEntityType = edmTypeMap[configuration.EntityType.FullName] as EdmEntityType;
                        IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                        entitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);
                        iter.Annotations.LinkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, configuration.GetNavigationPropertyLink(edmNavigationProperty.Name));
                    }
                }
            }
            return edmEntitySetMap;
        }
        public static IEdmModel BuildEdmModel(ODataModelBuilder builder)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }

            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName);

            // add types and sets, building an index on the way.
            Dictionary<string, IEdmStructuredType> edmTypeMap = model.AddTypes(builder.StructuralTypes);
            Dictionary<string, EdmEntitySet> edmEntitySetMap = model.AddEntitySets(builder.EntitySets, container, edmTypeMap);
            
            // add procedures
            container.AddProcedures(builder.Procedures, edmTypeMap, edmEntitySetMap);

            // finish up
            model.AddElement(container);
            return model;
        }
        public static IEdmModel SimpleCustomerOrderModel()
        {
            var model = new EdmModel();
            var customerType = new EdmEntityType("Default", "Customer");
            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            model.AddElement(customerType);

            var orderType = new EdmEntityType("Default", "Order");
            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(orderType);

            var addressType = new EdmComplexType("Default", "Address");
            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // Add navigations
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many });
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Customer", Target = customerType, TargetMultiplicity = EdmMultiplicity.One });

            var container = new EdmEntityContainer("Default", "Container");
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet = container.AddEntitySet("Orders", orderType);
            customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet);
            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet);

            Mock<IEntitySetLinkBuilder> linkAnnotation = new Mock<IEntitySetLinkBuilder>();
            model.SetEntitySetLinkBuilderAnnotation(customerSet, linkAnnotation.Object);
            model.SetEntitySetLinkBuilderAnnotation(orderSet, linkAnnotation.Object);

            model.AddElement(container);
            return model;
        }
 private static IEnumerable<Tuple<EdmEntitySet, EntitySetConfiguration>> AddEntitySets(IEnumerable<EntitySetConfiguration> entitySets, EdmEntityContainer container, Dictionary<Type, IEdmStructuredType> edmTypeMap)
 {
     return entitySets.Select(es => Tuple.Create(container.AddEntitySet(es, edmTypeMap), es));
 }
        [InlineData("Cars/NS.Vehicle/WashMultiple", "WashMultiple", "Collection([NS.Vehicle Nullable=False])")] // downcast
        public void ActionOverloadResoultionTests(string path, string actionName, string expectedEntityBound)
        {
            // Arrange
            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            var vehicle = new EdmEntityType("NS", "Vehicle");
            var car = new EdmEntityType("NS", "Car", vehicle);
            var motorcycle = new EdmEntityType("NS", "Motorcycle", vehicle);
            model.AddElements(new IEdmSchemaElement[] { vehicle, car, motorcycle, container });

            var washVehicle = AddBindableAction(container, "Wash", vehicle, isCollection: false);
            var washCar = AddBindableAction(container, "Wash", car, isCollection: false);
            var washVehicles = AddBindableAction(container, "WashMultiple", vehicle, isCollection: true);
            var washCars = AddBindableAction(container, "WashMultiple", car, isCollection: true);

            container.AddEntitySet("Vehicles", vehicle);
            container.AddEntitySet("Cars", car);
            container.AddEntitySet("Motorcycles", motorcycle);

            // Act
            ODataPath odataPath = _parser.Parse(model, path);

            // Assert
            Assert.NotNull(odataPath);
            ActionPathSegment actionSegment = Assert.IsType<ActionPathSegment>(odataPath.Segments.Last());
            Assert.Equal("NS.Container." + actionName, actionSegment.ActionName);
            Assert.Equal(expectedEntityBound, actionSegment.Action.Parameters.First().Type.Definition.ToTraceString());
        }
        private static void AddProcedures(this IEdmModel model, IEnumerable<ProcedureConfiguration> configurations, EdmEntityContainer container, Dictionary<Type, IEdmStructuredType> edmTypeMap, Dictionary<string, EdmEntitySet> edmEntitySetMap)
        {
            foreach (ProcedureConfiguration procedure in configurations)
            {
                switch (procedure.Kind)
                {
                    case ProcedureKind.Action:
                        ActionConfiguration action = procedure as ActionConfiguration;
                        IEdmTypeReference returnReference = GetEdmTypeReference(edmTypeMap, action.ReturnType, nullable: true);
                        IEdmExpression expression = GetEdmEntitySetExpression(edmEntitySetMap, action);

                        EdmFunctionImport functionImport = new EdmFunctionImport(container, action.Name, returnReference, expression, action.IsSideEffecting, action.IsComposable, action.IsBindable);
                        if (action.IsBindable)
                        {
                            model.SetIsAlwaysBindable(functionImport, action.IsAlwaysBindable);
                            if (action.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Entity)
                            {
                                Func<EntityInstanceContext, Uri> actionFactory = action.GetActionLink();
                                if (actionFactory != null)
                                {
                                    model.SetAnnotationValue<ActionLinkBuilder>(functionImport, new ActionLinkBuilder(actionFactory));
                                }
                            }
                        }

                        foreach (ParameterConfiguration parameter in action.Parameters)
                        {
                            // TODO: http://aspnetwebstack.codeplex.com/workitem/417
                            bool isParameterOptional = EdmLibHelpers.IsNullable(parameter.TypeConfiguration.ClrType);
                            IEdmTypeReference parameterTypeReference = GetEdmTypeReference(edmTypeMap, parameter.TypeConfiguration, nullable: isParameterOptional);
                            EdmFunctionParameter functionParameter = new EdmFunctionParameter(functionImport, parameter.Name, parameterTypeReference, EdmFunctionParameterMode.In);
                            functionImport.AddParameter(functionParameter);
                        }
                        container.AddElement(functionImport);
                        break;

                    case ProcedureKind.Function:
                        Contract.Assert(false, "Functions are not supported.");
                        break;

                    case ProcedureKind.ServiceOperation:
                        Contract.Assert(false, "ServiceOperations are not supported.");
                        break;
                }
            }
        }
        private static IEdmModel BuildModel(JObject viewObject, out IList<string> fieldsToIgnore)
        {
            var model = new EdmModel();
            fieldsToIgnore = new List<string>();

            var name = viewObject.PrimitivePropertyValue<string>("name");
            name = name.Replace(' ', '_');

            var entityType = new EdmEntityType(false, false, null, "OData4Socrata", name,
                                               Enumerable.Empty<IEdmStructuralProperty>());
            var entitySet = new EdmEntitySet(name, entityType);

            EdmComplexType phoneComplex = null;
            EdmComplexType locationComplex = null;
            EdmComplexType urlComplex = null;

            foreach (var column in viewObject.ArrayPropertyValue<JObject>("columns"))
            {
                var fieldName = column.PrimitivePropertyValue<string>("name");

                var sodaType = column.PrimitivePropertyValue<string>("dataTypeName");
                IEdmTypeReference typeReference;
                switch (sodaType)
                {
                    case "meta_data":
                        fieldsToIgnore.Add(fieldName);
                        continue;

                    case "text":

                        typeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string));
                        break;

                    case "url":
                        if (urlComplex == null)
                        {
                            urlComplex = new EdmComplexType(false, false, null, entityType.Namespace, "url");
                            new EdmStructuralProperty(urlComplex, "url", EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)),
                                                      null, EdmConcurrencyMode.None);
                            model.AddElement(urlComplex);
                        }

                        typeReference = new EdmComplexTypeReference(urlComplex, false);
                        break;

                    case "phone":
                        if (phoneComplex == null)
                        {
                            phoneComplex = new EdmComplexType(false, false, null, entityType.Namespace, "phone");
                            new EdmStructuralProperty(phoneComplex, "phone_number",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(phoneComplex, "phone_type",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            model.AddElement(phoneComplex);
                        }

                        typeReference = new EdmComplexTypeReference(phoneComplex, false);
                        break;

                    case "location":
                        if (locationComplex == null)
                        {
                            locationComplex = new EdmComplexType(false, false, null, entityType.Namespace, "location");
                            new EdmStructuralProperty(locationComplex, "human_address",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "latitude",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "longitude",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "machine_address",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "needs_recoding",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (bool)), null,
                                                      EdmConcurrencyMode.None);
                            model.AddElement(locationComplex);
                        }

                        typeReference = new EdmComplexTypeReference(locationComplex, false);
                        break;

                    default:
                        throw new Exception();
                }

                new EdmStructuralProperty(entityType, fieldName, typeReference, null, EdmConcurrencyMode.None);
            }

            model.AddElement(entityType);

            var entityContainer = new EdmEntityContainer();
            model.AddEntityContainer(entityContainer);
            entityContainer.AddElement(entitySet);

            return model;
        }
        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));
            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;
        }
        public void Invalid_CastTests(string path)
        {
            // Arrange
            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            var vehicle = new EdmEntityType("NS", "Vehicle");
            var car = new EdmEntityType("NS", "Car", vehicle);
            var motorcycle = new EdmEntityType("NS", "Motorcycle", vehicle);
            model.AddElements(new IEdmSchemaElement[] { vehicle, car, motorcycle, container });

            container.AddEntitySet("Vehicles", vehicle);
            container.AddEntitySet("Cars", car);
            container.AddEntitySet("Motorcycles", motorcycle);

            // Act
            var exception = Assert.Throws<ODataException>(() => _parser.Parse(model, path));
            Assert.Contains("Invalid cast encountered.", exception.Message);
        }
        private static IEdmFunctionImport AddBindableAction(EdmEntityContainer container, string name, IEdmEntityType bindingType, bool isCollection)
        {
            var action = container.AddFunctionImport(name, returnType: null, entitySet: null, sideEffecting: true, composable: false, bindable: true);

            IEdmTypeReference bindingParamterType = new EdmEntityTypeReference(bindingType, isNullable: false);
            if (isCollection)
            {
                bindingParamterType = new EdmCollectionTypeReference(
                    new EdmCollectionType(bindingParamterType), isNullable: false);
            }

            action.AddParameter("bindingParameter", bindingParamterType);
            return action;
        }
        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 CreateModel()
        {
            var model = new EdmModel();

            var orderType = new EdmEntityType("Default", "Order");
            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            model.AddElement(orderType);

            var customerType = new EdmEntityType("Default", "Customer");
            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            model.AddElement(customerType);

            // Add navigations
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Customer",
                Target = customerType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var container = new EdmEntityContainer("Default", "Container");
            var orderSet = container.AddEntitySet("Orders", orderType);
            var customerSet = container.AddEntitySet("Customers", customerType);

            container.AddFunctionImport("GetId", new EdmPrimitiveTypeReference(
                EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32), true));

            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"),
                customerSet);

            model.AddElement(container);
            return model;
        }
        public void CastTests(string path, string expectedEdmType)
        {
            // Arrange
            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            var vehicle = new EdmEntityType("NS", "Vehicle");
            var car = new EdmEntityType("NS", "Car", vehicle);
            var motorcycle = new EdmEntityType("NS", "Motorcycle", vehicle);
            model.AddElements(new IEdmSchemaElement[] { vehicle, car, motorcycle, container });

            container.AddEntitySet("Vehicles", vehicle);
            container.AddEntitySet("Cars", car);
            container.AddEntitySet("Motorcycles", motorcycle);

            // Act
            ODataPath odataPath = _parser.Parse(model, path);

            // Assert
            Assert.NotNull(odataPath);
            Assert.Equal(expectedEdmType, odataPath.EdmType.ToTraceString());
        }