Ejemplo n.º 1
0
        public void CanConfigureManyBinding_For_NavigationPropertiesInDerivedType()
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            builder
            .EntitySet <Vehicle>("vehicles")
            .HasManyBinding((Motorcycle m) => m.Manufacturers, "manufacturers");

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

            // Assert
            var vehicles = model.EntityContainer.FindEntitySet("vehicles");

            Assert.NotNull(vehicles);

            var motorcycle = model.AssertHasEntityType(typeof(Motorcycle));
            var motorcycleManufacturerProperty = motorcycle.AssertHasNavigationProperty(model, "Manufacturers", typeof(MotorcycleManufacturer), isNullable: false, multiplicity: EdmMultiplicity.Many);

            var motorcycleManufacturerPropertyTargetSet = vehicles.FindNavigationTarget(motorcycleManufacturerProperty);

            Assert.NotNull(motorcycleManufacturerPropertyTargetSet);
            Assert.Equal("manufacturers", motorcycleManufacturerPropertyTargetSet.Name);

            IEdmNavigationPropertyBinding binding = Assert.Single(vehicles.FindNavigationPropertyBindings(motorcycleManufacturerProperty));

            Assert.Same(motorcycleManufacturerPropertyTargetSet, binding.Target);
            Assert.Equal("Microsoft.AspNet.OData.Test.Builder.TestModels.Motorcycle/Manufacturers", binding.Path.Path);
        }
Ejemplo n.º 2
0
        public void CannotBindNavigationPropertyAutmatically_WhenMultipleEntitySetsOfPropertyType_Exist()
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            builder.EntitySet <Motorcycle>("motorcycles1").HasRequiredBinding(m => m.Manufacturer, "NorthWestMotorcycleManufacturers");
            builder.EntitySet <Motorcycle>("motorcycles2");
            builder.EntitySet <MotorcycleManufacturer>("NorthWestMotorcycleManufacturers");
            builder.EntitySet <MotorcycleManufacturer>("SouthWestMotorcycleManufacturers");

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

            // Assert
            IEdmEntitySet motorcycles1 = model.EntityContainer.FindEntitySet("motorcycles1");

            Assert.NotNull(motorcycles1);

            var motorcycle      = model.AssertHasEntityType(typeof(Motorcycle));
            var manufacturerNav = motorcycle.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: false, multiplicity: EdmMultiplicity.One);
            var bindings        = motorcycles1.FindNavigationPropertyBindings(manufacturerNav);
            IEdmNavigationPropertyBinding binding = Assert.Single(bindings);

            Assert.Equal("Manufacturer", binding.NavigationProperty.Name);
            Assert.Equal("NorthWestMotorcycleManufacturers", binding.Target.Name);
            Assert.Equal("Manufacturer", binding.Path.Path);

            IEdmEntitySet motorcycles2 = model.EntityContainer.FindEntitySet("motorcycles2");

            Assert.Null(motorcycles2.FindNavigationPropertyBindings(manufacturerNav));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Finds the navigation source that a navigation property targets.
        /// </summary>
        /// <param name="navigationProperty">The navigation property.</param>
        /// <param name="bindingPath">The binding path of the navigation property.</param>
        /// <returns>The navigation source that the navigation property targets</returns>
        public virtual IEdmNavigationSource FindNavigationTarget(IEdmNavigationProperty navigationProperty, IEdmPathExpression bindingPath)
        {
            EdmUtil.CheckArgumentNull(navigationProperty, "navigationProperty");

            bindingPath = bindingPath ?? new EdmPathExpression(navigationProperty.Name);

            Dictionary <string, IEdmNavigationPropertyBinding> result = EdmUtil.DictionarySafeGet(this.navigationPropertyMappings, navigationProperty);

            if (result != null)
            {
                IEdmNavigationPropertyBinding binding = EdmUtil.DictionarySafeGet(result, bindingPath.Path);
                {
                    if (binding != null)
                    {
                        return(binding.Target);
                    }
                }
            }

            if (navigationProperty.ContainsTarget)
            {
                return(AddNavigationPropertyBinding(
                           navigationProperty,
                           new EdmContainedEntitySet(this, navigationProperty, bindingPath),
                           bindingPath)
                       .Target);
            }

            return(EdmUtil.DictionaryGetOrUpdate(
                       this.unknownNavigationPropertyCache,
                       navigationProperty,
                       navProperty => new EdmUnknownEntitySet(this, navProperty)));
        }
Ejemplo n.º 4
0
        public void CanConfigureBindingPath_NavigationProperties_WithDerivedType()
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            var bindingConfiguration = builder
                                       .EntitySet <BindingCustomer>("Customers")
                                       .Binding
                                       .HasSinglePath((BindingVipCustomer v) => v.VipLocation);

            bindingConfiguration.HasOptionalBinding((BindingUsAddress u) => u.UsCity, "Cities_A");
            bindingConfiguration.HasManyBinding((BindingUsAddress u) => u.UsCities, "Cities_B");

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

            // Assert
            var customers = model.EntityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers);

            // "BindingVipCustomer" entity type
            var vipCustomer = model.AssertHasEntityType(typeof(BindingVipCustomer), typeof(BindingCustomer));

            Assert.Empty(vipCustomer.NavigationProperties());
            IEdmProperty vipLocationProperty = Assert.Single(vipCustomer.Properties());

            Assert.Equal("VipLocation", vipLocationProperty.Name);
            Assert.Equal(EdmPropertyKind.Structural, vipLocationProperty.PropertyKind);
            Assert.False(vipLocationProperty.Type.IsCollection());

            // "BindingUsAddress" complex type
            var usAddress    = model.AssertHasComplexType(typeof(BindingUsAddress), typeof(BindingAddress));
            var cityProperty = usAddress.AssertHasNavigationProperty(model, "UsCity", typeof(BindingCity), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);
            var bindings     = customers.FindNavigationPropertyBindings(cityProperty);
            IEdmNavigationPropertyBinding binding = Assert.Single(bindings);

            Assert.Equal("UsCity", binding.NavigationProperty.Name);
            Assert.Equal("Cities_A", binding.Target.Name);
            Assert.Equal("Microsoft.AspNet.OData.Test.Formatter.BindingVipCustomer/VipLocation/Microsoft.AspNet.OData.Test.Formatter.BindingUsAddress/UsCity", binding.Path.Path);

            IEdmNavigationSource navSource = customers.FindNavigationTarget(cityProperty, binding.Path);

            Assert.Same(navSource, binding.Target);

            var citiesProperty = usAddress.AssertHasNavigationProperty(model, "UsCities", typeof(BindingCity), isNullable: true, multiplicity: EdmMultiplicity.Many);

            bindings = customers.FindNavigationPropertyBindings(citiesProperty);
            binding  = Assert.Single(bindings);
            Assert.Equal("UsCities", binding.NavigationProperty.Name);
            Assert.Equal("Cities_B", binding.Target.Name);
            Assert.Equal("Microsoft.AspNet.OData.Test.Formatter.BindingVipCustomer/VipLocation/Microsoft.AspNet.OData.Test.Formatter.BindingUsAddress/UsCities", binding.Path.Path);

            navSource = customers.FindNavigationTarget(citiesProperty, binding.Path);
            Assert.Same(navSource, binding.Target);

            // "BindingCity" entity type
            model.AssertHasEntityType(typeof(BindingCity));
        }
Ejemplo n.º 5
0
        public static IEdmNavigationPropertyBinding AssertHasNavigationTarget(this IEdmNavigationSource navigationSource,
                                                                              IEdmNavigationProperty navigationProperty, string targetNavigationsource, string bindingPath)
        {
            IEdmNavigationPropertyBinding navMapping = navigationSource.NavigationPropertyBindings
                                                       .SingleOrDefault(n => n.NavigationProperty == navigationProperty && n.Path.Path == bindingPath);

            Assert.NotNull(navMapping);                                   // Guard
            Assert.Equal(targetNavigationsource, navMapping.Target.Name); // Guard

            return(navMapping);
        }
Ejemplo n.º 6
0
        private void WriteNavigationPropertyBinding(IEdmNavigationPropertyBinding binding)
        {
            this.xmlWriter.WriteStartElement(CsdlConstants.Element_NavigationPropertyBinding);

            this.WriteRequiredAttribute(CsdlConstants.Attribute_Path, binding.Path.Path, EdmValueWriter.StringAsXml);

            // TODO: handle container names, etc.
            this.WriteRequiredAttribute(CsdlConstants.Attribute_Target, binding.Target.Name, EdmValueWriter.StringAsXml);

            this.xmlWriter.WriteEndElement();
        }
Ejemplo n.º 7
0
        private IEdmNavigationPropertyBinding AddNavigationPropertyBinding(IEdmNavigationProperty navigationProperty, IEdmNavigationSource target, IEdmPathExpression bindingPath)
        {
            Dictionary <string, IEdmNavigationPropertyBinding> mapping = EdmUtil.DictionaryGetOrUpdate(
                this.navigationPropertyMappings,
                navigationProperty,
                navProperty => new Dictionary <string, IEdmNavigationPropertyBinding>());

            IEdmNavigationPropertyBinding containedBinding = EdmUtil.DictionaryGetOrUpdate <string, IEdmNavigationPropertyBinding>(
                mapping,
                bindingPath.Path,
                path => new EdmNavigationPropertyBinding(navigationProperty, target, new EdmPathExpression(path)));

            return(containedBinding);
        }
Ejemplo n.º 8
0
        public void CanConfigureManyProperty_MultipleBindingPath_For_NavigationProperties_WithComplex()
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            builder
            .EntitySet <BindingCustomer>("Customers")
            .Binding
            .HasManyPath(c => c.Addresses)
            .HasManyBinding(a => a.Cities, "Cities");

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

            // Assert
            var customers = model.EntityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers);

            // "BindingCustomer" entity type
            var customer = model.AssertHasEntityType(typeof(BindingCustomer));

            Assert.Empty(customer.NavigationProperties());
            IEdmProperty addressesProperty = Assert.Single(customer.Properties());

            Assert.Equal("Addresses", addressesProperty.Name);
            Assert.Equal(EdmPropertyKind.Structural, addressesProperty.PropertyKind);
            Assert.True(addressesProperty.Type.IsCollection());

            // "BindingAddress" complex type
            var address        = model.AssertHasComplexType(typeof(BindingAddress));
            var citiesProperty = address.AssertHasNavigationProperty(model, "Cities", typeof(BindingCity), isNullable: true, multiplicity: EdmMultiplicity.Many);
            var bindings       = customers.FindNavigationPropertyBindings(citiesProperty);
            IEdmNavigationPropertyBinding binding = Assert.Single(bindings);

            Assert.Equal("Cities", binding.NavigationProperty.Name);
            Assert.Equal("Cities", binding.Target.Name);
            Assert.Equal("Addresses/Cities", binding.Path.Path);

            IEdmNavigationSource navSource = customers.FindNavigationTarget(citiesProperty, binding.Path);

            Assert.Same(navSource, binding.Target);

            // "BindingCity" entity type
            model.AssertHasEntityType(typeof(BindingCity));
        }
Ejemplo n.º 9
0
        private void WriteNavigationPropertyBinding(IEdmNavigationPropertyBinding binding, IEdmEntityType entityType)
        {
            this.xmlWriter.WriteStartElement(CsdlConstants.Element_NavigationPropertyBinding);

            string path = binding.NavigationProperty.Name;

            if (!entityType.IsOrInheritsFrom(binding.NavigationProperty.DeclaringType))
            {
                path = binding.NavigationProperty.DeclaringEntityType().FullName() + '/' + path;
            }

            this.WriteRequiredAttribute(CsdlConstants.Attribute_Path, path, EdmValueWriter.StringAsXml);

            // TODO: handle container names, etc.
            this.WriteRequiredAttribute(CsdlConstants.Attribute_Target, binding.Target.Name, EdmValueWriter.StringAsXml);

            this.xmlWriter.WriteEndElement();
        }
Ejemplo n.º 10
0
        internal override void WriteNavigationPropertyBinding(IEdmNavigationPropertyBinding binding)
        {
            this.xmlWriter.WriteStartElement(CsdlConstants.Element_NavigationPropertyBinding);

            this.WriteRequiredAttribute(CsdlConstants.Attribute_Path, binding.Path.Path, EdmValueWriter.StringAsXml);

            // TODO: handle container names, etc.
            IEdmContainedEntitySet containedEntitySet = binding.Target as IEdmContainedEntitySet;

            if (containedEntitySet != null)
            {
                this.WriteRequiredAttribute(CsdlConstants.Attribute_Target, containedEntitySet.Path.Path, EdmValueWriter.StringAsXml);
            }
            else
            {
                this.WriteRequiredAttribute(CsdlConstants.Attribute_Target, binding.Target.Name, EdmValueWriter.StringAsXml);
            }

            this.xmlWriter.WriteEndElement();
        }
Ejemplo n.º 11
0
 internal void WriteNavigationPropertyBinding(IEdmNavigationSource navigationSource, IEdmNavigationPropertyBinding binding)
 {
     this.WriteNavigationPropertyBinding(binding, navigationSource.EntityType());
 }
Ejemplo n.º 12
0
 internal abstract void WriteNavigationPropertyBinding(IEdmNavigationPropertyBinding binding);
Ejemplo n.º 13
0
 internal void WriteNavigationPropertyBinding(IEdmNavigationSource navigationSource, IEdmNavigationPropertyBinding binding)
 {
     this.WriteNavigationPropertyBinding(binding);
 }
Ejemplo n.º 14
0
        public bool IsSkipNavigation(IEdmNavigationPropertyBinding bind)
        {
            var skipNavigation = DbContext.Model.FindEntityType(bind.NavigationProperty.DeclaringType.FullTypeName()).FindDeclaredSkipNavigation(bind.NavigationProperty.Name);

            return(skipNavigation != null);
        }