Example #1
0
        public void Apply_MultiForeignKeysOnForeignKeyProperty_Works()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();

            builder.EntityType <PrincipalEntity>().HasKey(p => new { p.PrincipalStringId, p.PrincipalIntId });
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(MultiDependentEntity2));

            PropertyInfo expectPropertyInfo1 = typeof(MultiDependentEntity2).GetProperty("PrincipalId1");
            PrimitivePropertyConfiguration propertyConfig1 = dependentEntity.AddProperty(expectPropertyInfo1);

            PropertyInfo expectPropertyInfo2 = typeof(MultiDependentEntity2).GetProperty("PrincipalId2");
            PrimitivePropertyConfiguration propertyConfig2 = dependentEntity.AddProperty(expectPropertyInfo2);

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

            navigation.AddedExplicitly = false;

            // Act
            ForeignKeyAttributeConvention convention = new ForeignKeyAttributeConvention();

            convention.Apply(propertyConfig1, dependentEntity, builder);
            convention.Apply(propertyConfig2, dependentEntity, builder);

            // 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);
        }
Example #2
0
        public void Apply_IgnoreEmptyAndUnknownForeignKeyProperty()
        {
            // Arrange
            Type dependentType = typeof(InvalidDependentEntity);

            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 = dependentType.GetProperty("Principal");
            NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(propertyInfo,
                                                                                               EdmMultiplicity.One);

            navigation.AddedExplicitly = false;

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

            // Assert
            Assert.Equal(2, navigation.DependentProperties.Count());
            Assert.Same(expectPropertyInfo1, navigation.DependentProperties.First());
            Assert.Same(expectPropertyInfo2, navigation.DependentProperties.Last());
        }
Example #3
0
        public void Apply_SingleNullableForeignKeyOnNavigationProperty_Works()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();

            builder.EntityType <PrincipalEntity>().HasKey(p => p.PrincipalIntId);
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(SingleNullableDependentEntity));

            PropertyInfo expectPrincipal = typeof(PrincipalEntity).GetProperty("PrincipalIntId");
            PropertyInfo expectDependent = typeof(SingleNullableDependentEntity).GetProperty("PrincipalId");
            PrimitivePropertyConfiguration primitiveProperty = dependentEntity.AddProperty(expectDependent);

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

            navigation.AddedExplicitly = false;

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

            // Assert
            PropertyInfo actualPropertyInfo = Assert.Single(navigation.DependentProperties);

            Assert.Same(expectDependent, actualPropertyInfo);

            actualPropertyInfo = Assert.Single(navigation.PrincipalProperties);
            Assert.Same(expectPrincipal, actualPropertyInfo);

            Assert.True(primitiveProperty.OptionalProperty);
        }
Example #4
0
        public void Apply_KeyNameConventions_Works()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            EntityTypeConfiguration principalEntity = builder.AddEntityType(typeof(DiscoveryPrincipalEntity));
            PropertyInfo            propertyInfo    = typeof(DiscoveryPrincipalEntity).GetProperty("DiscoveryPrincipalEntityId");

            principalEntity.HasKey(propertyInfo);

            EntityTypeConfiguration dependentEntity    = builder.AddEntityType(typeof(DiscoveryDependentEntity));
            PropertyInfo            expectPropertyInfo = typeof(DiscoveryDependentEntity).GetProperty("DiscoveryPrincipalEntityId");

            dependentEntity.AddProperty(expectPropertyInfo);

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

            navigation.AddedExplicitly = false;

            // Act
            new ForeignKeyDiscoveryConvention().Apply(navigation, dependentEntity, builder);

            // Assert
            PropertyInfo actualPropertyInfo = Assert.Single(navigation.DependentProperties);

            Assert.Same(expectPropertyInfo, actualPropertyInfo);

            PropertyInfo principalProperty = Assert.Single(navigation.PrincipalProperties);

            Assert.Equal("DiscoveryPrincipalEntityId", principalProperty.Name);
        }
Example #5
0
        public void Apply_TypeNameConventions_Works(string propertyName, EdmMultiplicity multiplicity, bool optional)
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            EntityTypeConfiguration principalEntity = builder.AddEntityType(typeof(DiscoveryPrincipalEntity));
            PropertyInfo            propertyInfo    = typeof(DiscoveryPrincipalEntity).GetProperty(propertyName);

            principalEntity.HasKey(propertyInfo);

            EntityTypeConfiguration dependentEntity    = builder.AddEntityType(typeof(DiscoveryDependentEntity));
            PropertyInfo            expectPropertyInfo = typeof(DiscoveryDependentEntity).GetProperty("DiscoveryPrincipalEntity" + propertyName);
            var property = dependentEntity.AddProperty(expectPropertyInfo);

            PropertyInfo navigationPropertyInfo        = typeof(DiscoveryDependentEntity).GetProperty("Principal");
            NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(navigationPropertyInfo, multiplicity);

            // Act
            new ForeignKeyDiscoveryConvention().Apply(navigation, dependentEntity, builder);

            // Assert
            PropertyInfo actualPropertyInfo = Assert.Single(navigation.DependentProperties);

            Assert.Same(expectPropertyInfo, actualPropertyInfo);

            PropertyInfo principalProperty = Assert.Single(navigation.PrincipalProperties);

            Assert.Equal(propertyName, principalProperty.Name);

            Assert.Equal(optional, property.OptionalProperty);
        }
Example #6
0
        public void Apply_SingleForeignKeyOnForeignKeyProperty_Works()
        {
            // Arrange
            Type dependentType = typeof(SingleDependentEntity2);

            ODataModelBuilder builder = new ODataModelBuilder();

            builder.Entity <PrincipalEntity>().HasKey(p => p.PrincipalIntId);

            EntityTypeConfiguration dependentEntity = builder.AddEntity(dependentType);

            PropertyInfo expectPropertyInfo = dependentType.GetProperty("PrincipalId");
            PrimitivePropertyConfiguration primitiveProperty = dependentEntity.AddProperty(expectPropertyInfo);

            PropertyInfo propertyInfo = dependentType.GetProperty("Principal");
            NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(propertyInfo,
                                                                                               EdmMultiplicity.ZeroOrOne);

            navigation.AddedExplicitly = false;

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

            // Assert
            PropertyInfo actualPropertyInfo = Assert.Single(navigation.DependentProperties);

            Assert.Same(expectPropertyInfo, actualPropertyInfo);

            Assert.Equal("PrincipalIntId", Assert.Single(navigation.PrincipalProperties).Name);
            Assert.False(primitiveProperty.OptionalProperty);
        }
        public void Apply_DoesnotRemove_ExplicitlyAddedProperties()
        {
            // Arrange
            ODataConventionModelBuilder builder  = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing: false);
            PropertyInfo            propertyInfo = typeof(TestEntity).GetProperty("ExplicitlyAddedProperty");
            EntityTypeConfiguration entity       = builder.AddEntityType(typeof(TestEntity));
            PropertyConfiguration   property     = entity.AddProperty(propertyInfo);

            // Act
            _convention.Apply(entity, builder);

            // Assert
            Assert.Contains(propertyInfo, entity.ExplicitProperties.Keys);
            Assert.DoesNotContain(propertyInfo, entity.RemovedProperties);
        }
Example #8
0
        public void Apply_DoesnotRemove_ExplicitlyAddedProperties()
        {
            // Arrange
            ODataModelBuilder       builder      = new ODataModelBuilder();
            PropertyInfo            propertyInfo = typeof(TestEntity).GetProperty("Property");
            EntityTypeConfiguration entity       = builder.AddEntity(typeof(TestEntity));
            PropertyConfiguration   property     = entity.AddProperty(propertyInfo);

            // Act
            new NotMappedAttributeConvention().Apply(property, entity);

            // Assert
            Assert.Contains(propertyInfo, entity.ExplicitProperties.Keys);
            Assert.DoesNotContain(propertyInfo, entity.RemovedProperties);
        }
        public void Apply_DoesnotRemove_ExplicitlyAddedProperties()
        {
            // Arrange
            ODataConventionModelBuilder builder  = ODataConventionModelBuilderFactory.Create();
            PropertyInfo            propertyInfo = typeof(TestEntity).GetProperty("ExplicitlyAddedProperty");
            EntityTypeConfiguration entity       = builder.AddEntityType(typeof(TestEntity));
            PropertyConfiguration   property     = entity.AddProperty(propertyInfo);

            // Act
            new IgnoreDataMemberAttributeEdmPropertyConvention().Apply(property, entity, builder);

            // Assert
            Assert.Contains(propertyInfo, entity.ExplicitProperties.Keys);
            Assert.DoesNotContain(propertyInfo, entity.RemovedProperties);
        }