private static Microsoft.OData.Edm.IEdmModel GetImplicitEdmModel(WebRouteConfiguration configuration)
        {
            ODataConventionModelBuilder modelBuilder = configuration.CreateConventionModelBuilder();
            var products = modelBuilder.EntitySet <Product>("ConditionalLinkGeneration_Products");

            products.HasEditLink(
                ctx => { return((Uri)null); },
                false);
            products.HasReadLink(
                ctx =>
            {
                object id;
                ctx.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(ResourceContextHelper.CreateODataLink(ctx,
                                                                     new EntitySetSegment(ctx.NavigationSource as IEdmEntitySet),
                                                                     new KeySegment(new[] { new KeyValuePair <string, object>("Id", id) }, ctx.StructuredType as IEdmEntityType, null))));
            },
                true);

            // Navigation Property is not working because of bug: http://aspnetwebstack.codeplex.com/workitem/780
            //products.HasNavigationPropertiesLink(
            //    products.EntityType.NavigationProperties,
            //    (ctx, nav) => null,
            //    false);

            modelBuilder.EntitySet <Supplier>("ConditionalLinkGeneration_Suppliers");
            modelBuilder.EntitySet <ProductFamily>("ConditionalLinkGeneration_ProductFamilies");

            var model = modelBuilder.GetEdmModel();

            return(model);
        }
Пример #2
0
        public static IEdmModel GetEdmModel(WebRouteConfiguration configuration)
        {
            var builder = configuration.CreateConventionModelBuilder();

            builder.EntitySet <MovingObject>("InheritanceTests_MovingObjects");
            builder.EntitySet <Vehicle>("InheritanceTests_Vehicles");
            builder.EntitySet <MiniSportBike>("InheritanceTests_MiniSportBikes");

            var cars = builder.EntitySet <Car>("InheritanceTests_Cars");

            cars.EntityType.Action("Wash");

            builder.OnModelCreating = mb =>
            {
                cars.HasNavigationPropertiesLink(
                    cars.EntityType.NavigationProperties,
                    (entityContext, navigationProperty) =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("Id", out id);
                    return(new Uri(ResourceContextHelper.CreateODataLink(entityContext,
                                                                         new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                         new KeySegment(new[] { new KeyValuePair <string, object>("Id", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                         new NavigationPropertySegment(navigationProperty, null))));
                },
                    false);
            };

            builder.EntityType <SportBike>().Action("Wash");
            builder.EntitySet <Customer>("InheritanceTests_Customers");

            return(builder.GetEdmModel());
        }
Пример #3
0
        public void ChangeNavigationLinkShouldWork()
        {
            ODataConventionModelBuilder mb = new ODataConventionModelBuilder();
            var products = mb.EntitySet <Product>("Products");

            mb.OnModelCreating = mb2 =>
            {
                products.HasNavigationPropertiesLink(
                    products.EntityType.NavigationProperties,
                    (entityContext, navigationProperty) =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                    return(new Uri(ResourceContextHelper.CreateODataLink(entityContext,
                                                                         new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                         new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                         new NavigationPropertySegment(navigationProperty, null))));
                },
                    false);
            };

            var model = mb.GetEdmModel();
        }
Пример #4
0
        private static IEdmModel GetActionsModel(WebRouteConfiguration configuration)
        {
            ODataModelBuilder builder         = configuration.CreateConventionModelBuilder();
            var baseEntitySet                 = builder.EntitySet <BaseEntity>("BaseEntity");
            var alwaysAvailableActionBaseType = baseEntitySet.EntityType.Action("AlwaysAvailableActionBaseType");
            var transientActionBaseType       = baseEntitySet.EntityType.Action("TransientActionBaseType");

            Func <ResourceContext, Uri> transientActionBaseTypeLinkFactory = eic =>
            {
                IEdmEntityType baseType = eic.EdmModel.FindType(typeof(BaseEntity).FullName) as IEdmEntityType;
                object         id;
                eic.EdmObject.TryGetPropertyValue("Id", out id);
                if (!eic.StructuredType.IsOrInheritsFrom(baseType) || (int)id % 2 == 1)
                {
                    return(null);
                }
                else
                {
                    // find the action
                    var action = eic.EdmModel.SchemaElements
                                 .Where(elem => elem.Name == "TransientActionBaseType")
                                 .Cast <EdmAction>()
                                 .FirstOrDefault();

                    var segments = new List <ODataPathSegment>();
                    segments.Add(new EntitySetSegment(eic.NavigationSource as IEdmEntitySet));
                    segments.Add(new KeySegment(new[] { new KeyValuePair <string, object>("Id", id) }, eic.StructuredType as IEdmEntityType, null));
                    var    pathHandler = eic.Request.GetPathHandler();
                    string link        = ResourceContextHelper.CreateODataLink(eic, "Actions", pathHandler, segments);
                    link += "/" + action.FullName();
                    return(new Uri(link));
                }
            };

            transientActionBaseType.HasActionLink(transientActionBaseTypeLinkFactory, true);
            var derivedEntityType = builder.EntityType <DerivedEntity>().DerivesFrom <BaseEntity>();
            var alwaysAvailableActionDerivedType = derivedEntityType.Action("AlwaysAvailableActionDerivedType");
            var transientActionDerivedType       = derivedEntityType.Action("TransientActionDerivedType");
            Func <ResourceContext, Uri> transientActionDerivedTypeLinkFactory = eic =>
            {
                IEdmEntityType derivedType = eic.EdmModel.FindType(typeof(DerivedEntity).FullName) as IEdmEntityType;
                object         id;
                eic.EdmObject.TryGetPropertyValue("Id", out id);
                if (!eic.StructuredType.IsOrInheritsFrom(derivedType) || (int)id % 2 == 1)
                {
                    return(null);
                }
                else
                {
                    // find the action
                    var action = eic.EdmModel.SchemaElements
                                 .Where(elem => elem.Name == "TransientActionDerivedType")
                                 .Cast <EdmAction>()
                                 .FirstOrDefault();

                    var segments = new List <ODataPathSegment>();
                    segments.Add(new EntitySetSegment(eic.NavigationSource as IEdmEntitySet));
                    segments.Add(new KeySegment(new[] { new KeyValuePair <string, object>("Id", id) }, eic.StructuredType as IEdmEntityType, null));
                    segments.Add(new TypeSegment(derivedType, null));
                    // bug 1985: Make the internal constructor as public in BoundActionPathSegment
                    //segments.Add(new BoundActionPathSegment(action));
                    var    pathHandler = eic.Request.GetPathHandler();
                    string link        = ResourceContextHelper.CreateODataLink(eic, "Actions", pathHandler, segments);
                    link += "/" + action.FullName();
                    return(new Uri(link));
                }
            };

            transientActionDerivedType.HasActionLink(transientActionDerivedTypeLinkFactory, true);
            return(builder.GetEdmModel());
        }
Пример #5
0
        private static IEdmModel GetExplicitEdmModel()
        {
            var modelBuilder = new ODataModelBuilder();

            var enumContry = modelBuilder.EnumType <CountryOrRegion>();

            enumContry.Member(CountryOrRegion.Canada);
            enumContry.Member(CountryOrRegion.China);
            enumContry.Member(CountryOrRegion.India);
            enumContry.Member(CountryOrRegion.Japen);
            enumContry.Member(CountryOrRegion.USA);

            var products            = modelBuilder.EntitySet <Product>("Products");
            var toiletPaperSupplier = modelBuilder.EntityType <ToiletPaperSupplier>();

            products.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var mainSupplier = modelBuilder.Singleton <Supplier>("MainSupplier");

            var suppliers = modelBuilder.EntitySet <Supplier>("Suppliers").HasDerivedTypeConstraint <ToiletPaperSupplier>();

            mainSupplier.HasDerivedTypeConstraints(typeof(ToiletPaperSupplier));

            suppliers.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var families = modelBuilder.EntitySet <ProductFamily>("ProductFamilies");

            families.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var product = products.EntityType;

            product.HasKey(p => p.ID);
            product.Property(p => p.Name);
            product.Property(p => p.ReleaseDate);
            product.Property(p => p.SupportedUntil);

            var address = modelBuilder.ComplexType <Address>();

            address.Property(a => a.City);
            address.Property(a => a.CountryOrRegion);
            address.Property(a => a.State);
            address.Property(a => a.Street);
            address.Property(a => a.ZipCode);

            var supplier = suppliers.EntityType;

            supplier.HasKey(s => s.ID);
            supplier.Property(s => s.Name);
            supplier.CollectionProperty(s => s.Addresses);
            supplier.CollectionProperty(s => s.Tags);
            supplier.EnumProperty(s => s.CountryOrRegion);
            supplier.ComplexProperty(s => s.MainAddress).HasDerivedTypeConstraints(typeof(Address));

            var productFamily = families.EntityType;

            productFamily.HasKey(pf => pf.ID);
            productFamily.Property(pf => pf.Name);
            productFamily.Property(pf => pf.Description);

            // Create relationships and bindings in one go
            products.HasRequiredBinding(p => p.Family, families);
            families.HasManyBinding(pf => pf.Products, products);
            families.HasOptionalBinding(pf => pf.Supplier, suppliers).NavigationProperty.HasDerivedTypeConstraint <ToiletPaperSupplier>();
            suppliers.HasManyBinding(s => s.ProductFamilies, families);

            // Create navigation Link builders
            products.HasNavigationPropertiesLink(
                product.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            families.HasNavigationPropertiesLink(
                productFamily.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            suppliers.HasNavigationPropertiesLink(
                supplier.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(
                                   ODataTestConstants.DefaultRouteName,
                                   new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            var function = supplier.Function("GetAddress").Returns <Address>().HasDerivedTypeConstraintForReturnType <Address>();

            function.ReturnTypeConstraints.Location = Microsoft.OData.Edm.Csdl.EdmVocabularyAnnotationSerializationLocation.OutOfLine;
            function.Parameter <int>("value");

            var action = modelBuilder.Action("GetAddress").Returns <Address>().HasDerivedTypeConstraintsForReturnType(typeof(Address));

            action.Parameter <Supplier>("supplier").HasDerivedTypeConstraint <ToiletPaperSupplier>();
            action.ReturnTypeConstraints.Location = Microsoft.OData.Edm.Csdl.EdmVocabularyAnnotationSerializationLocation.OutOfLine;

            return(modelBuilder.GetEdmModel());
        }
Пример #6
0
        public static IEdmModel GetExplicitModel(bool foreignKey)
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            var customer = builder.EntityType <ForeignKeyCustomer>();

            customer.HasKey(c => c.Id);
            customer.Property(c => c.Name);
            customer.HasMany(c => c.Orders);

            var order = builder.EntityType <ForeignKeyOrder>();

            order.HasKey(o => o.OrderId);
            order.Property(o => o.OrderName);
            order.Property(o => o.CustomerId);

            EntitySetConfiguration <ForeignKeyCustomer> customers;
            EntitySetConfiguration <ForeignKeyOrder>    orders;

            if (foreignKey)
            {
                order.HasOptional(o => o.Customer, (o, c) => o.CustomerId == c.Id).CascadeOnDelete(cascade: true);

                customers = builder.EntitySet <ForeignKeyCustomer>("ForeignKeyCustomers");
                orders    = builder.EntitySet <ForeignKeyOrder>("ForeignKeyOrders");
                customers.HasManyBinding(c => c.Orders, "ForeignKeyOrders");
                orders.HasOptionalBinding(o => o.Customer, "ForeignKeyCustomers");
            }
            else
            {
                order.HasOptional(o => o.Customer);

                customers = builder.EntitySet <ForeignKeyCustomer>("ForeignKeyCustomersNoCascade");
                orders    = builder.EntitySet <ForeignKeyOrder>("ForeignKeyOrdersNoCascade");
                customers.HasManyBinding(c => c.Orders, "ForeignKeyOrdersNoCascade");
                orders.HasOptionalBinding(o => o.Customer, "ForeignKeyCustomersNoCascade");
            }

            customers.HasIdLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("Id", out id);
                string uri = ResourceContextHelper.CreateODataLink(entityContext,
                                                                   new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                   new KeySegment(new[] { new KeyValuePair <string, object>("Id", id) }, entityContext.StructuredType as IEdmEntityType, null));
                return(new Uri(uri));
            }, true);

            orders.HasIdLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("OrderId", out id);
                string uri = ResourceContextHelper.CreateODataLink(entityContext,
                                                                   new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                   new KeySegment(new[] { new KeyValuePair <string, object>("OrderId", id) }, entityContext.StructuredType as IEdmEntityType, null));
                return(new Uri(uri));
            }, true);

            // Create navigation Link builders
            customers.HasNavigationPropertiesLink(
                customer.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("Id", out id);
                string uri = ResourceContextHelper.CreateODataLink(entityContext,
                                                                   new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                   new KeySegment(new[] { new KeyValuePair <string, object>("Id", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                   new NavigationPropertySegment(navigationProperty, null));
                return(new Uri(uri));
            }, true);

            orders.HasNavigationPropertiesLink(
                order.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("OrderId", out id);
                string uri = ResourceContextHelper.CreateODataLink(entityContext,
                                                                   new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                   new KeySegment(new[] { new KeyValuePair <string, object>("OrderId", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                   new NavigationPropertySegment(navigationProperty, null));
                return(new Uri(uri));
            }, true);

            BuildAction(builder);

            return(builder.GetEdmModel());
        }
        private static IEdmModel GetExplicitEdmModel()
        {
            var modelBuilder = new ODataModelBuilder();

            var enumContry = modelBuilder.EnumType <CountryOrRegion>();

            enumContry.Member(CountryOrRegion.Canada);
            enumContry.Member(CountryOrRegion.China);
            enumContry.Member(CountryOrRegion.India);
            enumContry.Member(CountryOrRegion.Japen);
            enumContry.Member(CountryOrRegion.USA);

            var products = modelBuilder.EntitySet <Product>("Products");

            products.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var suppliers = modelBuilder.EntitySet <Supplier>("Suppliers");

            suppliers.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var families = modelBuilder.EntitySet <ProductFamily>("ProductFamilies");

            families.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var product = products.EntityType;

            product.HasKey(p => p.ID);
            product.Property(p => p.Name);
            product.Property(p => p.ReleaseDate);
            product.Property(p => p.SupportedUntil);

            var address = modelBuilder.ComplexType <Address>();

            address.Property(a => a.City);
            address.Property(a => a.CountryOrRegion);
            address.Property(a => a.State);
            address.Property(a => a.Street);
            address.Property(a => a.ZipCode);

            var supplier = suppliers.EntityType;

            supplier.HasKey(s => s.ID);
            supplier.Property(s => s.Name);
            supplier.CollectionProperty(s => s.Addresses);
            supplier.CollectionProperty(s => s.Tags);
            supplier.EnumProperty(s => s.CountryOrRegion);

            var productFamily = families.EntityType;

            productFamily.HasKey(pf => pf.ID);
            productFamily.Property(pf => pf.Name);
            productFamily.Property(pf => pf.Description);

            // Create relationships and bindings in one go
            products.HasRequiredBinding(p => p.Family, families);
            families.HasManyBinding(pf => pf.Products, products);
            families.HasOptionalBinding(pf => pf.Supplier, suppliers);
            suppliers.HasManyBinding(s => s.ProductFamilies, families);

            // Create navigation Link builders
            products.HasNavigationPropertiesLink(
                product.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            families.HasNavigationPropertiesLink(
                productFamily.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            suppliers.HasNavigationPropertiesLink(
                supplier.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(
                                   ODataTestConstants.DefaultRouteName,
                                   new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            return(modelBuilder.GetEdmModel());
        }