Ejemplo n.º 1
0
        public void GetTargetNavigationSource_Returns_BaseTypeNavigationSource_IfNoMatchingEntitysetForCurrentType()
        {
            // Arrange
            ODataModelBuilder       builder                = new ODataModelBuilder();
            EntityTypeConfiguration motorcycle             = builder.AddEntityType(typeof(Motorcycle));
            EntityTypeConfiguration manufacturer           = builder.AddEntityType(typeof(Manufacturer));
            EntityTypeConfiguration motorcycleManufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer)).DerivesFrom(manufacturer);

            NavigationPropertyConfiguration navigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);
            EntitySetConfiguration          manufacturers      = builder.AddEntitySet("manufacturers", manufacturer);

            // Act
            NavigationSourceConfiguration targetNavigationSource = AssociationSetDiscoveryConvention.GetTargetNavigationSource(navigationProperty, builder);

            // Assert
            Assert.Same(manufacturers, targetNavigationSource);
        }
        public void AssociationSetDiscoveryConvention_AddsBindingForBaseAndDerivedNavigationProperties()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            EntityTypeConfiguration vehicle = builder.AddEntityType(typeof(Vehicle));

            EntityTypeConfiguration         car = builder.AddEntityType(typeof(Car)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration carNavigationProperty = car.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration         motorcycle = builder.AddEntityType(typeof(Motorcycle)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration motorcycleNavigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration manufacturer = builder.AddEntityType(typeof(Manufacturer));

            builder.AddEntityType(typeof(MotorcycleManufacturer)).DerivesFrom(manufacturer);
            builder.AddEntityType(typeof(CarManufacturer)).DerivesFrom(manufacturer);

            EntitySetConfiguration manufacturers = builder.AddEntitySet("manufacturers", manufacturer);

            Mock <EntitySetConfiguration> entitySet = new Mock <EntitySetConfiguration>(MockBehavior.Strict);

            entitySet.Setup(v => v.EntityType).Returns(vehicle);
            entitySet.Setup(
                v =>
                v.AddBinding(motorcycleNavigationProperty, manufacturers,
                             new List <MemberInfo> {
                typeof(Motorcycle), typeof(Motorcycle).GetProperty("Manufacturer")
            }))
            .Returns <NavigationPropertyConfiguration>(null);
            entitySet.Setup(
                v =>
                v.AddBinding(carNavigationProperty, manufacturers,
                             new List <MemberInfo> {
                typeof(Car), typeof(Car).GetProperty("Manufacturer")
            }))
            .Returns <NavigationPropertyConfiguration>(null);

            // Act
            _convention.Apply(entitySet.Object, builder);

            // Assert
            entitySet.VerifyAll();
        }
        public void Apply_ActionOnDeleteAttribute_Works()
        {
            // Arrange
            Type orderType = typeof(TestOrder);
            ODataConventionModelBuilder builder          = new ODataConventionModelBuilder();
            PropertyInfo                    propertyInfo = orderType.GetProperty("Customer");
            EntityTypeConfiguration         entity       = builder.AddEntityType(orderType);
            NavigationPropertyConfiguration navProperty  = entity.AddNavigationProperty(propertyInfo, EdmMultiplicity.One);

            navProperty.AddedExplicitly = false;
            navProperty.HasConstraint(orderType.GetProperty("CustomerId"),
                                      typeof(TestCustomer).GetProperty("Id"));

            // Act
            new ActionOnDeleteAttributeConvention().Apply(navProperty, entity, builder);

            // Assert
            Assert.Equal(EdmOnDeleteAction.Cascade, navProperty.OnDeleteAction);
        }
        public void Apply_AddsNavigationLinkFor_AllBaseDeclaredAndDerivedProperties()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            EntityTypeConfiguration vehicle = builder.AddEntityType(typeof(Vehicle));

            EntityTypeConfiguration         car = builder.AddEntityType(typeof(Car)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration carNavigationProperty = car.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration         motorcycle = builder.AddEntityType(typeof(Motorcycle)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration motorcycleNavigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration manufacturer           = builder.AddEntityType(typeof(Manufacturer));
            EntityTypeConfiguration motorcycleManufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer)).DerivesFrom(manufacturer);
            EntityTypeConfiguration carManufacturer        = builder.AddEntityType(typeof(CarManufacturer)).DerivesFrom(manufacturer);

            EntitySetConfiguration manufacturers = builder.AddEntitySet("manufacturers", manufacturer);


            Mock <EntitySetConfiguration> entitySet = new Mock <EntitySetConfiguration>(MockBehavior.Strict);

            entitySet.Setup(v => v.EntityType).Returns(vehicle);
            entitySet.Setup(v => v.GetNavigationPropertyLink(motorcycleNavigationProperty)).Returns <NavigationPropertyConfiguration>(null);
            entitySet.Setup(v => v.GetNavigationPropertyLink(carNavigationProperty)).Returns <NavigationPropertyConfiguration>(null);

            entitySet
            .Setup(v => v.HasNavigationPropertyLink(motorcycleNavigationProperty, It.IsAny <NavigationLinkBuilder>()))
            .Callback((NavigationPropertyConfiguration property, NavigationLinkBuilder navBuilder) => Assert.True(navBuilder.FollowsConventions))
            .Returns <EntitySetConfiguration>(null);
            entitySet
            .Setup(v => v.HasNavigationPropertyLink(carNavigationProperty, It.IsAny <NavigationLinkBuilder>()))
            .Returns <EntitySetConfiguration>(null);

            // Act
            _convention.Apply(entitySet.Object, builder);

            // Assert
            entitySet.VerifyAll();
        }
Ejemplo n.º 5
0
        public void Apply_MultiForeignKeysOnNavigationProperty_Works()
        {
            // Arrange
            Type dependentType = typeof(MultiDependentEntity);

            ODataModelBuilder builder = new ODataModelBuilder();

            builder.Entity <PrincipalEntity>().HasKey(p => new { p.PrincipalIntId, p.PrincipalStringId });

            EntityTypeConfiguration dependentEntity = builder.AddEntity(dependentType);

            PropertyInfo expectPropertyInfo1 = dependentType.GetProperty("PrincipalId1");

            dependentEntity.AddProperty(expectPropertyInfo1);

            PropertyInfo expectPropertyInfo2 = dependentType.GetProperty("PrincipalId2");

            dependentEntity.AddProperty(expectPropertyInfo2);

            PropertyInfo propertyInfo = typeof(MultiDependentEntity).GetProperty("Principal");
            NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(propertyInfo,
                                                                                               EdmMultiplicity.One);

            navigation.AddedExplicitly = false;

            // Act
            new ForeignKeyAttributeConvention().Apply(navigation, dependentEntity);

            // Assert
            Assert.Equal(2, navigation.DependentProperties.Count());
            Assert.Same(expectPropertyInfo1, navigation.DependentProperties.First());
            Assert.Same(expectPropertyInfo2, navigation.DependentProperties.Last());

            Assert.Equal(2, navigation.PrincipalProperties.Count());
            Assert.Equal("PrincipalIntId", navigation.PrincipalProperties.First().Name);
            Assert.Equal("PrincipalStringId", navigation.PrincipalProperties.Last().Name);
        }
        public void AddNavigationProperty_ThrowsIfTypeIsDateTime(Type propertyType, EdmMultiplicity multiplicity)
        {
            // Arrange
            MockType         type     = new MockType("Customer", @namespace: "Contoso");
            MockPropertyInfo property = new MockPropertyInfo(propertyType, "Birthday");

            property.SetupGet(p => p.ReflectedType).Returns(type);
            property.SetupGet(p => p.DeclaringType).Returns(type);

            Mock <EntityTypeConfiguration> mock = new Mock <EntityTypeConfiguration> {
                CallBase = true
            };
            EntityTypeConfiguration configuration = mock.Object;

            mock.SetupGet(c => c.ClrType).Returns(type);

            // Act & Assert
            Assert.ThrowsArgument(
                () => configuration.AddNavigationProperty(property, multiplicity),
                "navigationProperty",
                string.Format(
                    "The type '{0}' of property 'Birthday' in the 'Contoso.Customer' type is not a supported type.",
                    propertyType.FullName));
        }