public static IDictionary <PropertyInfo, PropertyInfo> GetSelectedProperty(Expression exp)
        {
            PropertyPairSelectorVisitor visitor = new PropertyPairSelectorVisitor();

            visitor.Visit(exp);
            return(visitor.Properties);
        }
        public void LambdaExpressionHasNotTwoParameters_ThrowException()
        {
            // Arrange
            Expression<Func<Dependent, int>> expr = d => d.ForeignKey1;

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => PropertyPairSelectorVisitor.GetSelectedProperty(expr),
                SRResources.LambdaExpressionMustHaveExactlyTwoParameters);
        }
        public void CanWork_WithNullValue()
        {
            // Arrange & Act
            IDictionary<PropertyInfo, PropertyInfo> properties =
                PropertyPairSelectorVisitor.GetSelectedProperty(null);

            // Assert
            Assert.Equal(0, properties.Count);
        }
        public void LambdaExpressionAccessesNotProperty_ThrowException()
        {
            // Arrange
            Expression<Func<Dependent, Principal, bool>> expr = (d, p) => d.ForeignKey1 == p.Field;

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => PropertyPairSelectorVisitor.GetSelectedProperty(expr),
                string.Format("Member '{0}.Field' is not a property.", typeof(Principal).FullName));
        }
        public void UnsupportedExpressionNodeType_ThrowException()
        {
            // Arrange
            Expression<Func<Dependent, Principal, bool>> expr = (d, p) => d.ForeignKey1 > p.PrincipalKey1;

            // Act & Assert
            Assert.Throws<NotSupportedException>(() => PropertyPairSelectorVisitor.GetSelectedProperty(expr),
                "Unsupported Expression NodeType 'GreaterThan'.");
        }
        public void MemberExpressionNotBoundToParameter_ThrowException()
        {
            // Arrange
            Expression<Func<Dependent, Principal, bool>> expr =
                (d, p) => new Dependent().ForeignKey1 == new Principal().PrincipalKey1;

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => PropertyPairSelectorVisitor.GetSelectedProperty(expr),
                "MemberExpressions must be bound to the LambdaExpression parameter.");
        }
        public void MemberExpressionTypeNotMatch_ThrowException()
        {
            // Arrange
            Expression<Func<Dependent, Principal, bool>> expr =
                (d, p) => d.ForeignKey4 == p.PrincipalKey1;

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => PropertyPairSelectorVisitor.GetSelectedProperty(expr),
                String.Format(SRResources.EqualExpressionsMustHaveSameTypes,
                    typeof(Dependent).FullName, "ForeignKey4", "System.Int16",
                    typeof(Principal).FullName, "PrincipalKey1", "System.Int32"));
        }
        public void CanGetSingleSelectedPropertyPair()
        {
            // Arrange
            Expression<Func<Dependent, Principal, bool>> expr = (d, p) => d.ForeignKey1 == p.PrincipalKey1;

            // Act
            IDictionary<PropertyInfo, PropertyInfo> properties =
                PropertyPairSelectorVisitor.GetSelectedProperty(expr);

            // Assert
            Assert.Equal(1, properties.Count);
            Assert.Equal("ForeignKey1", properties.Keys.First().Name);
            Assert.Equal("PrincipalKey1", properties.Values.First().Name);
        }
        public void CanGetMultiSelectedPropertyPairs()
        {
            // Arrange
            Expression<Func<Dependent, Principal, bool>> expr =
                (d, p) => d.ForeignKey1 == p.PrincipalKey1 && d.ForeignKey2 == p.PrincipalKey2;

            // Act
            IDictionary<PropertyInfo, PropertyInfo> properties =
                PropertyPairSelectorVisitor.GetSelectedProperty(expr);

            // Assert
            Assert.Equal(2, properties.Count);

            Assert.Equal("PrincipalKey1", properties[typeof(Dependent).GetProperty("ForeignKey1")].Name);
            Assert.Equal("PrincipalKey2", properties[typeof(Dependent).GetProperty("ForeignKey2")].Name);
        }
示例#10
0
        public NavigationPropertyConfiguration HasRequired <TTargetEntity>(
            Expression <Func <TEntityType, TTargetEntity> > navigationPropertyExpression,
            Expression <Func <TEntityType, TTargetEntity, bool> > referentialConstraintExpression) where TTargetEntity : class
        {
            NavigationPropertyConfiguration navigation = GetOrCreateNavigationProperty(navigationPropertyExpression,
                                                                                       EdmMultiplicity.One);

            IDictionary <PropertyInfo, PropertyInfo> referentialConstraints =
                PropertyPairSelectorVisitor.GetSelectedProperty(referentialConstraintExpression);

            foreach (KeyValuePair <PropertyInfo, PropertyInfo> constraint in referentialConstraints)
            {
                navigation.HasConstraint(constraint);
            }

            return(navigation);
        }