예제 #1
0
        internal static void AssertHasContainment(IEdmEntityType myOrder, IEdmModel model)
        {
            var orderLines = myOrder.AssertHasNavigationProperty(
                model,
                "OrderLines",
                typeof(BuilderTestModels.OrderLine),
                isNullable: false,
                multiplicity: EdmMultiplicity.Many);

            Assert.True(orderLines.ContainsTarget);

            var orderHeader = myOrder.AssertHasNavigationProperty(
                model,
                "OrderHeader",
                typeof(BuilderTestModels.OrderHeader),
                isNullable: false,
                multiplicity: EdmMultiplicity.One);

            Assert.True(orderHeader.ContainsTarget);

            var orderCancellation = myOrder.AssertHasNavigationProperty(
                model,
                "OrderCancellation",
                typeof(BuilderTestModels.OrderCancellation),
                isNullable: true,
                multiplicity: EdmMultiplicity.ZeroOrOne);

            Assert.True(orderCancellation.ContainsTarget);
        }
        public void DataMemberAttributeEdmPropertyConvention_PropertyAliased_IfEnabled(string propertyAlias, bool modelAliasing)
        {
            // Arrange
            MockType relatedType =
                new MockType("RelatedEntity")
                .Property <int>("ID");
            MockType type =
                new MockType("Entity")
                .Property(typeof(int), "ID")
                .Property(relatedType, "RelatedEntity", new DataMemberAttribute {
                Name = "AliasRelatedEntity"
            });

            type.Setup(t => t.GetCustomAttributes(It.IsAny <Type>(), It.IsAny <bool>()))
            .Returns(new[] { new DataContractAttribute() });

            // Act
            ODataConventionModelBuilder builder = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing);

            builder.AddEntityType(type);
            IEdmModel model = builder.GetEdmModel();

            // Assert
            IEdmEntityType entity = model.AssertHasEntityType(type);

            entity.AssertHasKey(model, "ID", EdmPrimitiveTypeKind.Int32);
            entity.AssertHasNavigationProperty(
                model,
                propertyAlias,
                relatedType,
                isNullable: true,
                multiplicity: EdmMultiplicity.ZeroOrOne);
        }
예제 #3
0
        public void GetEdmModel_CanSetDependentPropertyNullable_ForOptionalNavigation()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <User> userType = builder.EntityType <User>();

            userType.HasKey(c => c.UserId).HasMany(c => c.Roles);
            userType.Property(u => u.StringPrincipal);

            EntityTypeConfiguration <Role> roleType = builder.EntityType <Role>();

            roleType.HasKey(r => r.RoleId)
            .HasOptional(r => r.User, (r, u) => r.UserStringForeignKey == u.StringPrincipal);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.NotNull(model);
            IEdmEntityType roleEntityType = model.AssertHasEntityType(typeof(Role));

            roleEntityType.AssertHasNavigationProperty(model, "User", typeof(User), isNullable: true,
                                                       multiplicity: EdmMultiplicity.ZeroOrOne);

            IEdmProperty edmProperty = Assert.Single(roleEntityType.Properties().Where(c => c.Name == "UserStringForeignKey"));

            Assert.True(edmProperty.Type.IsNullable);
        }
        public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <SportBike>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet           = model.EntityContainers().Single().FindEntitySet("vehicles");
            IEdmEntityType         sportbikeType                  = model.AssertHasEntityType(typeof(SportBike));
            IEdmNavigationProperty motorcycleManufacturerProperty = sportbikeType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();
            string            routeName     = "Route";

            configuration.Routes.MapODataRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.SetConfiguration(configuration);
            request.SetODataRouteName(routeName);

            EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            var serializerContext = new ODataSerializerContext {
                Model = model, EntitySet = vehiclesEdmEntitySet, Url = request.GetUrlHelper()
            };
            var entityContext = new EntityInstanceContext(serializerContext, sportbikeType.AsReference(), new SportBike {
                Model = 2009, Name = "Ninja"
            });

            Uri uri = linkBuilder.BuildNavigationLink(entityContext, motorcycleManufacturerProperty, ODataMetadataLevel.Default);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Ninja')/Manufacturer", uri.AbsoluteUri);
        }
예제 #5
0
        public void GetEdmModel_Works_WithSingleForeignKeyProperty()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <User> userType = builder.Entity <User>();

            userType.HasKey(u => u.UserId).HasMany(u => u.Roles);

            EntityTypeConfiguration <Role> roleType = builder.Entity <Role>();

            roleType.HasKey(r => r.RoleId)
            .HasRequired(r => r.User, (r, u) => r.UserForeignKey == u.UserId)
            .CascadeOnDelete();

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.NotNull(model);
            IEdmEntityType roleEntityType = model.AssertHasEntityType(typeof(Role));

            IEdmNavigationProperty usersNav = roleEntityType.AssertHasNavigationProperty(model, "User",
                                                                                         typeof(User), false, EdmMultiplicity.One);

            Assert.Equal(EdmOnDeleteAction.Cascade, usersNav.OnDelete);

            IEdmStructuralProperty dependentProperty = Assert.Single(usersNav.DependentProperties);

            Assert.Equal("UserForeignKey", dependentProperty.Name);

            IEdmProperty edmProperty = Assert.Single(roleEntityType.Properties().Where(c => c.Name == "UserForeignKey"));

            Assert.False(edmProperty.Type.IsNullable);
        }
        public void NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Vehicle>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet = model.EntityContainers().Single().FindEntitySet("vehicles");
            IEdmEntityType         carType = model.AssertHasEntityType(typeof(Car));
            IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.Routes.MapHttpRoute(ODataRouteNames.PropertyNavigationWithCast, "{controller}({parentid})/{entitytype}/{navigationproperty}");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            IEntitySetLinkBuilder linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            Uri uri = linkBuilder.BuildNavigationLink(
                new EntityInstanceContext(model, vehiclesEdmEntitySet, carType, request.GetUrlHelper(), new Car {
                Model = 2009, Name = "Accord"
            }),
                carManufacturerProperty);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri);
        }
        public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <SportBike>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet           = model.EntityContainers().Single().FindEntitySet("vehicles");
            IEdmEntityType         sportbikeType                  = model.AssertHasEntityType(typeof(SportBike));
            IEdmNavigationProperty motorcycleManufacturerProperty = sportbikeType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.Routes.MapHttpRoute(ODataRouteNames.PropertyNavigation, "{controller}({parentid})/{navigationproperty}");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            IEntitySetLinkBuilder linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            Uri uri = linkBuilder.BuildNavigationLink(
                new EntityInstanceContext(model, vehiclesEdmEntitySet, sportbikeType, request.GetUrlHelper(), new SportBike {
                Model = 2009, Name = "Ninja"
            }),
                motorcycleManufacturerProperty);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Ninja')/Manufacturer", uri.AbsoluteUri);
        }
예제 #8
0
        public void ModelBuilder_BindsToTheClosestEntitySet_ForNavigationProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Vehicle>("vehicles");
            builder.EntitySet <CarManufacturer>("car_manufacturers");
            builder.EntitySet <MotorcycleManufacturer>("motorcycle_manufacturers");

            IEdmModel model = builder.GetEdmModel();

            model.AssertHasEntitySet("vehicles", typeof(Vehicle));
            IEdmEntitySet vehicles = model.EntityContainers().Single().FindEntitySet("vehicles");

            IEdmEntityType car        = model.AssertHasEntityType(typeof(Car));
            IEdmEntityType motorcycle = model.AssertHasEntityType(typeof(Motorcycle));
            IEdmEntityType sportbike  = model.AssertHasEntityType(typeof(SportBike));

            Assert.Equal(2, vehicles.NavigationTargets.Count());
            vehicles.AssertHasNavigationTarget(
                car.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne),
                "car_manufacturers");
            vehicles.AssertHasNavigationTarget(
                motorcycle.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne),
                "motorcycle_manufacturers");
            vehicles.AssertHasNavigationTarget(
                sportbike.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne),
                "motorcycle_manufacturers");
        }
예제 #9
0
        public void CanBuildModelForAnonymousTypes()
        {
            Type entityType = new
            {
                ID = default(int),
                ComplexCollection = new[]
                {
                    new { ComplexProperty = default(string) }
                },
                NavigationCollection = new[]
                {
                    new { ID = default(int) }
                }
            }.GetType();

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.AddEntitySet("entityset", builder.AddEntity(entityType));

            IEdmModel model = builder.GetEdmModel();

            IEdmEntityType entity = model.AssertHasEntitySet("entityset", entityType);

            entity.AssertHasKey(model, "ID", EdmPrimitiveTypeKind.Int32);
            entity.AssertHasCollectionProperty(model, "ComplexCollection", new { ComplexProperty = default(string) }.GetType(), isNullable: true);
            entity.AssertHasNavigationProperty(model, "NavigationCollection", new { ID = default(int) }.GetType(), isNullable: false, multiplicity: EdmMultiplicity.ZeroOrOne);

            IEdmComplexType complexType = model.AssertHasComplexType(new { ComplexProperty = default(string) }.GetType());

            complexType.AssertHasPrimitiveProperty(model, "ComplexProperty", EdmPrimitiveTypeKind.String, isNullable: true);
        }
예제 #10
0
        public void NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties()
        {
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <Vehicle>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles");
            IEdmEntityType         carType = model.AssertHasEntityType(typeof(Car));
            IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            var    configuration = RoutingConfigurationFactory.Create();
            string routeName     = "Route";

            configuration.MapODataServiceRoute(routeName, null, model);

            var request = RequestFactory.Create(HttpMethod.Get, "http://localhost", configuration, routeName);

            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);

            linkBuilder.AddNavigationPropertyLinkBuilder(carManufacturerProperty, new NavigationLinkBuilder((context, property) => context.GenerateNavigationPropertyLink(property, includeCast: true), false));
            var serializerContext = ODataSerializerContextFactory.Create(model, vehiclesEdmEntitySet, request);
            var entityContext     = new ResourceContext(serializerContext, carType.AsReference(), new Car {
                Model = 2009, Name = "Accord"
            });

            Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.MinimalMetadata);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/Microsoft.AspNet.OData.Test.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri);
        }
예제 #11
0
        public void GetEdmModel_CanSetReferentialConstraint_WithCustomPrincipal()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <User> userType = builder.EntityType <User>();

            userType.HasKey(c => c.UserId).HasMany(c => c.Roles);

            EntityTypeConfiguration <Role> roleType = builder.EntityType <Role>();

            roleType.HasKey(r => r.RoleId)
            .HasRequired(r => r.User, (r, u) => r.UserForeignKey == u.NotKeyPrincipal)
            .CascadeOnDelete(false);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.NotNull(model);
            IEdmEntityType roleEntityType = model.AssertHasEntityType(typeof(Role));

            IEdmNavigationProperty usersNav = roleEntityType.AssertHasNavigationProperty(model, "User",
                                                                                         typeof(User), isNullable: false, multiplicity: EdmMultiplicity.One);

            Assert.Equal(EdmOnDeleteAction.None, usersNav.OnDelete);

            IEdmStructuralProperty dependentProperty = Assert.Single(usersNav.DependentProperties());

            Assert.Equal("UserForeignKey", dependentProperty.Name);

            IEdmStructuralProperty principalProperty = Assert.Single(usersNav.PrincipalProperties());

            Assert.Equal("NotKeyPrincipal", principalProperty.Name);
        }
        public void NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Vehicle>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles");
            IEdmEntityType         carType = model.AssertHasEntityType(typeof(Car));
            IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();
            string            routeName     = "Route";

            configuration.MapODataServiceRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.SetConfiguration(configuration);
            request.ODataProperties().RouteName = routeName;

            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);

            var serializerContext = new ODataSerializerContext {
                Model = model, NavigationSource = vehiclesEdmEntitySet, Url = request.GetUrlHelper()
            };
            var entityContext = new EntityInstanceContext(serializerContext, carType.AsReference(), new Car {
                Model = 2009, Name = "Accord"
            });

            Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.FullMetadata);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.OData.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri);
        }
예제 #13
0
        internal static void AssertHasAdditionalContainment(IEdmEntityType mySpecialOrder, IEdmModel model)
        {
            var gift = mySpecialOrder.AssertHasNavigationProperty(
                model,
                "Gift",
                typeof(BuilderTestModels.Gift),
                isNullable: true,
                multiplicity: EdmMultiplicity.ZeroOrOne);

            Assert.True(gift.ContainsTarget);
        }
        public void RequiredAttributeEdmPropertyConvention_DoesnotOverwriteExistingConfigurationForNavigationProperties()
        {
            MockType anotherType =
                new MockType("RelatedEntity")
                .Property <int>("ID");

            MockType type =
                new MockType("Entity")
                .Property(typeof(int), "ID")
                .Property(anotherType, "RelatedEntity", new RequiredAttribute());

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.AddEntityType(type).AddNavigationProperty(type.GetProperty("RelatedEntity"), EdmMultiplicity.ZeroOrOne);

            IEdmModel      model  = builder.GetEdmModel();
            IEdmEntityType entity = model.AssertHasEntityType(type);

            entity.AssertHasNavigationProperty(model, "RelatedEntity", anotherType, isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);
        }
        public void RequiredAttributeEdmPropertyConvention_ConfiguresRequiredNavigationPropertyAsRequired()
        {
            MockType anotherType =
                new MockType("RelatedEntity")
                .Property <int>("ID");

            MockType type =
                new MockType("Entity")
                .Property(typeof(int), "ID")
                .Property(anotherType, "RelatedEntity", new RequiredAttribute());

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.AddEntityType(type);

            IEdmModel      model  = builder.GetEdmModel();
            IEdmEntityType entity = model.AssertHasEntityType(type);

            entity.AssertHasNavigationProperty(model, "RelatedEntity", anotherType, isNullable: false, multiplicity: EdmMultiplicity.One);
        }
예제 #16
0
        public void ModelBuilder_Doesnot_Override_NavigationPropertyConfiguration()
        {
            MockType type1 =
                new MockType("Entity1")
                .Property <int>("ID");

            MockType type2 =
                new MockType("Entity2")
                .Property <int>("ID")
                .Property(type1, "Relation");

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.AddEntity(type2).AddNavigationProperty(type2.GetProperty("Relation"), EdmMultiplicity.One);

            IEdmModel      model  = builder.GetEdmModel();
            IEdmEntityType entity = model.AssertHasEntityType(type2);

            // bug in edmlib causing this to ZeroOrOne.
            entity.AssertHasNavigationProperty(model, "Relation", type1, isNullable: false, multiplicity: EdmMultiplicity.ZeroOrOne);
        }
        public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForDeclaredProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Car>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet = model.EntityContainers().Single().FindEntitySet("vehicles");
            IEdmEntityType         carType = model.AssertHasEntityType(typeof(Car));
            IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();
            string            routeName     = "Route";

            configuration.Routes.MapODataRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.SetConfiguration(configuration);
            request.SetODataRouteName(routeName);

            EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            Uri uri = linkBuilder.BuildNavigationLink(
                new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = vehiclesEdmEntitySet,
                EntityType     = carType,
                Url            = request.GetUrlHelper(),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                carManufacturerProperty,
                ODataMetadataLevel.Default);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/Manufacturer", uri.AbsoluteUri);
        }
        public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForDeclaredProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Car>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet = model.EntityContainers().Single().FindEntitySet("vehicles");
            IEdmEntityType         carType = model.AssertHasEntityType(typeof(Car));
            IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.EnableOData(model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            IEntitySetLinkBuilder linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            Uri uri = linkBuilder.BuildNavigationLink(
                new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = vehiclesEdmEntitySet,
                EntityType     = carType,
                UrlHelper      = request.GetUrlHelper(),
                PathHandler    = new DefaultODataPathHandler(model),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                carManufacturerProperty);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/Manufacturer", uri.AbsoluteUri);
        }
예제 #19
0
        public void GetEdmModel_Works_WithMultiForeignKeys()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <MultiUser> userType = builder.Entity <MultiUser>();

            userType.HasKey(u => new { u.UserId1, u.UserId2 })
            .HasMany(u => u.Roles);

            EntityTypeConfiguration <MultiRole> roleType = builder.Entity <MultiRole>();

            roleType.HasKey(r => r.RoleId)
            .HasOptional(r => r.User, (r, u) => r.UserKey1 == u.UserId1 && r.UserKey2 == u.UserId2)
            .CascadeOnDelete();

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.NotNull(model);
            IEdmEntityType roleEntityType = model.AssertHasEntityType(typeof(MultiRole));

            IEdmNavigationProperty usersNav = roleEntityType.AssertHasNavigationProperty(model, "User",
                                                                                         typeof(MultiUser), true, EdmMultiplicity.ZeroOrOne);

            Assert.Equal(EdmOnDeleteAction.Cascade, usersNav.OnDelete);

            Assert.Equal(2, usersNav.DependentProperties.Count());
            Assert.Equal("UserKey1", usersNav.DependentProperties.First().Name);
            Assert.Equal("UserKey2", usersNav.DependentProperties.Last().Name);

            IEdmProperty edmProperty = Assert.Single(roleEntityType.Properties().Where(c => c.Name == "UserKey1"));

            Assert.False(edmProperty.Type.IsNullable);

            edmProperty = Assert.Single(roleEntityType.Properties().Where(c => c.Name == "UserKey2"));
            Assert.False(edmProperty.Type.IsNullable);
        }
예제 #20
0
        public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties()
        {
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <SportBike>("vehicles");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmEntitySet          vehiclesEdmEntitySet           = model.EntityContainer.FindEntitySet("vehicles");
            IEdmEntityType         sportbikeType                  = model.AssertHasEntityType(typeof(SportBike));
            IEdmNavigationProperty motorcycleManufacturerProperty = sportbikeType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            var    configuration = RoutingConfigurationFactory.Create();
            string routeName     = "Route";

            configuration.MapODataServiceRoute(routeName, null, model);

            var request = RequestFactory.Create(HttpMethod.Get, "http://localhost", configuration, routeName);

            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);

            linkBuilder.AddNavigationPropertyLinkBuilder(motorcycleManufacturerProperty, new NavigationLinkBuilder((context, property) => context.GenerateNavigationPropertyLink(property, includeCast: false), false));
            var serializerContext = ODataSerializerContextFactory.Create(model, vehiclesEdmEntitySet, request);
            var entityContext     = new ResourceContext(serializerContext, sportbikeType.AsReference(), new SportBike {
                Model = 2009, Name = "Ninja"
            });

            // We might get one of these:
            // http://localhost/vehicles(Model=2009,Name='Ninja')/Manufacturer
            // http://localhost/vehicles(Name='Ninja',Model=2009)/Manufacturer
            Uri    uri        = linkBuilder.BuildNavigationLink(entityContext, motorcycleManufacturerProperty, ODataMetadataLevel.MinimalMetadata);
            string aboluteUri = uri.AbsoluteUri;

            Assert.Contains("http://localhost/vehicles(", aboluteUri);
            Assert.Contains("Model=2009", aboluteUri);
            Assert.Contains("Name='Ninja'", aboluteUri);
            Assert.Contains(")/Manufacturer", aboluteUri);
        }
예제 #21
0
        public void GetEdmModel_CanSetMultiReferentialConstraint_WithCustomPrincipal()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <MultiUser> userType = builder.EntityType <MultiUser>();

            userType.HasKey(c => c.UserId1)
            .HasKey(c => c.UserId2)
            .HasMany(c => c.Roles);

            EntityTypeConfiguration <MultiRole> roleType = builder.EntityType <MultiRole>();

            roleType.HasKey(r => r.RoleId)
            .HasRequired(r => r.User, (r, u) => r.UserKey1 == u.PrincipalUserKey1 && r.UserKey2 == u.PrincipalUserKey2)
            .CascadeOnDelete();

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.NotNull(model);
            IEdmEntityType roleEntityType = model.AssertHasEntityType(typeof(MultiRole));

            IEdmNavigationProperty usersNav = roleEntityType.AssertHasNavigationProperty(model, "User",
                                                                                         typeof(MultiUser), isNullable: false, multiplicity: EdmMultiplicity.One);

            Assert.Equal(EdmOnDeleteAction.Cascade, usersNav.OnDelete);

            Assert.Equal(2, usersNav.DependentProperties().Count());
            Assert.Equal("UserKey1", usersNav.DependentProperties().First().Name);
            Assert.Equal("UserKey2", usersNav.DependentProperties().Last().Name);

            Assert.Equal(2, usersNav.PrincipalProperties().Count());
            Assert.Equal("PrincipalUserKey1", usersNav.PrincipalProperties().First().Name);
            Assert.Equal("PrincipalUserKey2", usersNav.PrincipalProperties().Last().Name);
        }
        public void DataMemberAttributeEdmPropertyConvention_ConfiguresNavigationDataMembers(bool isRequired)
        {
            MockType relatedType =
                new MockType("RelatedEntity")
                .Property <int>("ID");
            MockType type =
                new MockType("Entity")
                .Property(typeof(int), "ID")
                .Property(relatedType, "RelatedEntity", new DataMemberAttribute {
                IsRequired = isRequired
            });

            type.Setup(t => t.GetCustomAttributes(It.IsAny <Type>(), It.IsAny <bool>())).Returns(new[] { new DataContractAttribute() });

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.AddEntity(type);

            IEdmModel      model  = builder.GetEdmModel();
            IEdmEntityType entity = model.AssertHasEntityType(type);

            entity.AssertHasKey(model, "ID", EdmPrimitiveTypeKind.Int32);
            entity.AssertHasNavigationProperty(model, "RelatedEntity", relatedType, isNullable: !isRequired, multiplicity: isRequired ? EdmMultiplicity.One : EdmMultiplicity.ZeroOrOne);
        }
예제 #23
0
        public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForDeclaredProperties_OnSingleton()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.Singleton <Car>("Contoso");
            builder.EntitySet <Manufacturer>("manufacturers");

            IEdmModel              model = builder.GetEdmModel();
            IEdmSingleton          vehicleEdmSingleton     = model.EntityContainer.FindSingleton("Contoso");
            IEdmEntityType         carType                 = model.AssertHasEntityType(typeof(Car));
            IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();
            string            routeName     = "Route";

            configuration.MapODataServiceRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.SetConfiguration(configuration);
            request.EnableODataDependencyInjectionSupport(routeName);

            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehicleEdmSingleton);

            linkBuilder.AddNavigationPropertyLinkBuilder(carManufacturerProperty, new NavigationLinkBuilder((context, property) => context.GenerateNavigationPropertyLink(property, includeCast: false), false));
            var serializerContext = new ODataSerializerContext {
                Model = model, NavigationSource = vehicleEdmSingleton, Url = request.GetUrlHelper()
            };
            var entityContext = new ResourceContext(serializerContext, carType.AsReference(), new Car {
                Model = 2014, Name = "Contoso2014"
            });

            Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.MinimalMetadata);

            Assert.Equal("http://localhost/Contoso/Manufacturer", uri.AbsoluteUri);
        }
        internal static void AssertHasContainment(IEdmEntityType myOrder, IEdmModel model)
        {
            var orderLines = myOrder.AssertHasNavigationProperty(
                model,
                "OrderLines",
                typeof(BuilderTestModels.OrderLine),
                isNullable: false,
                multiplicity: EdmMultiplicity.Many);
            Assert.True(orderLines.ContainsTarget);

            var orderHeader = myOrder.AssertHasNavigationProperty(
                model,
                "OrderHeader",
                typeof(BuilderTestModels.OrderHeader),
                isNullable: false,
                multiplicity: EdmMultiplicity.One);
            Assert.True(orderHeader.ContainsTarget);

            var orderCancellation = myOrder.AssertHasNavigationProperty(
                model,
                "OrderCancellation",
                typeof(BuilderTestModels.OrderCancellation),
                isNullable: true,
                multiplicity: EdmMultiplicity.ZeroOrOne);
            Assert.True(orderCancellation.ContainsTarget);
        }
 internal static void AssertHasAdditionalContainment(IEdmEntityType mySpecialOrder, IEdmModel model)
 {
     var gift = mySpecialOrder.AssertHasNavigationProperty(
         model,
         "Gift",
         typeof(BuilderTestModels.Gift),
         isNullable: true,
         multiplicity: EdmMultiplicity.ZeroOrOne);
     Assert.True(gift.ContainsTarget);
 }