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

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

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => PropertyPairSelectorVisitor.GetSelectedProperty(expr),
                                                      SRResources.LambdaExpressionMustHaveExactlyTwoParameters);
        }
示例#3
0
        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'.");
        }
示例#4
0
        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));
        }
示例#5
0
        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.");
        }
示例#6
0
        public void CanWork_WithNullValue()
        {
            // Arrange
            Expression <Func <Dependent, Principal, bool> > expr = null;

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

            // Assert
            Assert.Equal(0, properties.Count);
        }
示例#7
0
        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"));
        }
示例#8
0
        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);
        }
示例#9
0
        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);
        }
        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);
        }